How Objects of class are created in C++?
I am having a problem in creating object of class in C++. I want to know how to create it? As, it is the base of the programming language which is not only used in C++ but it is used in every programming language. So, I am desperately willing to know the solution to this question. IF anyone having any information about it then please let know as soon as possible.
Re: How Objects of class are created in C++?
You should know that class is user defined data type which encapsulates data and member function together. The objects are the run time entity. It has the physical existence. The object of the class is created in a very similar way as we create the variable of any built in data type(like int ,float).
The Object is created by writing the name of the class followed by the name of the object. More than one Object can be created on the same line or separate line.
The general syntax is
classname objectname1,objectname2…;
Re: How Objects of class are created in C++?
Code:
For Example
Box b1,b2;
Here b1 and b2 are the objects of class Box.
For example
class Box
{
private : int l,h,w;
public :
void get_data()
{
cout<<”enter the length width and height of box”;
cin>>l>>h>>w;
}
void display()
{
cout<<”the length of box is<<l;
cout<<”the height of box is<<h;
cout<<”the width of box is<<w;
}
};
main ()
{
Box b1,b2;
}
In above example b1 and b2 are the two objects of the class Box. Here the object b1 and b2 both have their own copy of variables l, h, w ,and created on the same line. The Object can be created on separate line
Box b1;
Box b2;
There is another place where objects can be created i.e. at the time of defining a class by placing objects name after the ending brace but before the semicolon of class.
class Box
{
members
}b1,b2;
Re: How Objects of class are created in C++?
Well, you should know that when you are creating a class you should be aware that it should follow the class of the object. This is very much important when objects of class are created in the class. Well, many of them get confused while creation of object. The best part is that you can create one object or more than that in same or separate line.
Re: How Objects of class are created in C++?
It is important to know how object of class are created in the class because if you are not able to create a object in class then how would you able execute or write the code for different functions where in creation of object is must. So, one gets struck when is not able to do so. I think that you can go through the above example which can help you to understand this term better.