/    /  CPP – Creating Objects

Creating Objects

 

Once a class has been declared, variables may be created with the class name. The class variables are known as objects. Declaring an object is similar to declaring a variable in any basic data type. The necessary storage space is then assigned to an object. The object serves to access the members of the class. We can declare several objects in one instruction.

 

For example, Student S1, S2, S3; // S1, S2, and S3 are three type student objects.

Objects can also be declared at the end of class specification as,

class student

{
int rno;
char name[10];
public:
void get();
void put();
}s1,s2;

 

Accessing class members

The class’s private members can be accessed only by public members and are not accessible by the object.
The public member function is accessed by an object as-

object-name.function-name(actual-arguments);

 

Access to a data member is solely dependent on the data member’s access control. If it is public, then the data member can be readily accessed by using the direct member access operator (.) with the object of this class. If the data member is defined as private or protected, we do not have direct access to the data variables. Then we will have to create special public functions to access, use or initialize private and protected data members.

object-name.member-name;