Lab10

Home Up

 

 Lab1 ] Lab2 ] Lab3 ] lab4 ] lab5 ] lab6 ] Lab7 ] Lab8 ] Lab9 ] [ Lab10 ]

CP107  LAB 10

 

LAB OBJECTIVES:

Lab Objectives:

To be able to create a structure and access it members
To be able to create an array of structures and access data members using the "." notation and with pointers
To be able to create a simple class
To be able to create private and public data member and member functions
Understand constructor and destructor
To be able to access object's private data members and using a member function

 

LAB ASSIGNMENTS struct.cpp and myClass.cpp

  1. Create a struct that has the following data structure
    struct userInfo
    {
        int userID;
        char name[40];
        double current_balance;
        bool credit_risk;
    };

    1.a) Create an array of two structures, assign arbitrary values to each member of the structure, then print out the values. Use "." notation for all operations (accessing the data members and assigning values to data members). See the example in Chapter 6 slides.

    1.b) Repeat what you did in 1.a, but this time, first dynamically allocate memory for for two structure using the new operator.  Assign values to the data members using the arrow notation (pointer to structure) and then display each data member of both structure using a pointer to the structure.

    Note: You must use a loop for displaying values of the data member of structure in both 1.a and 1.b

  2. Create a class myClass that has the following
    A private variable named x
    A constructor, myClass(int y) that prints "Hello from the constructor"
    and initializes the class' private data x with the value of y (argument in constructor)
    A destructor that prints "Goodbye from the destructor"
    A public function printData that prints the value of the value of private data x

    Make two objects, obj1(10), obj2(20) from the class, myClass. Call the printData member function of each object and print the value of the object's private data.

 

 

Copyright (c) Yusuf Family Website 2001