Results 1 to 5 of 5

Thread: How Objects of class are created in C++?

  1. #1
    Join Date
    Mar 2010
    Posts
    393

    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.

  2. #2
    Join Date
    May 2009
    Posts
    637

    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…;

  3. #3
    Join Date
    May 2009
    Posts
    527

    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;

  4. #4
    Join Date
    Apr 2009
    Posts
    569

    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.

  5. #5
    Join Date
    May 2009
    Posts
    539

    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.

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  2. Instantiate different objects of same class
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 09-03-2010, 10:05 AM
  3. Accessing Objects Outside Of Class
    By Level8 in forum Software Development
    Replies: 4
    Last Post: 22-02-2010, 08:22 PM
  4. How to sort vector objects by class member variables in c++?
    By Linoo in forum Software Development
    Replies: 4
    Last Post: 29-01-2010, 07:15 PM
  5. Good news for CBSE CLASS X & CLASS IX - visit learnnext
    By surya_expert in forum Education Career and Job Discussions
    Replies: 0
    Last Post: 13-12-2008, 12:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,485,676.58121 seconds with 16 queries