Classes and Objects....cont(3)

Scope Rules of Classes at a Glance

Objects

The declaration of a class does not include objects of that class. It only specifies the type of information the objects of this class type will hold, when created. Objects of a class can be created using the class tag-name as type specifier. The syntax used to declare objects is:

class-name object1, object2, .....objectn;

For example:

class book;

{

public;

int bookno;

float cost;

void getdata(int i, float j)

{

bookno=1;

cost=j;

}

void putdata(void)

{

cout <<"\nBook no:"<<bookno;

cout<<"\nCost:"<<cost;

};

book r1, r2; // objects of type book

int main()

{

r1.getdata(101, 123.50); // Assign values to data members of object r1

r2.getdata(102, 145.50); // Assign values to data members of object r2

r1.putdata(); // display values of r1

r2.putdata(); // display values of r2

}

The public members of an object can be accessed directly by using the objects name and dot(.) operator.

HOME LEARN C++ PREVIOUS NEXT