answersLogoWhite

0


Best Answer

An object is a thing that occupies memory, has a value, and has methods that manipulate it. As such, even elementary variables are objects.

Most, however, consider that an object in C++ is an instance of a class type.

// declaration and definition

class myClass {

... attributes

... methods

... constructors

... destructor

};

// instantiation by direct allocation

myClass myClassObject;

// instantiation by heap allocation

myClass *myClassObjectPtr = new myClass;

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

You declare objects in exactly the same way you declare primitive data types:

int x; // declare an instance of int and name it x.

myObject y; // declare an instance of myObject and name it y.

The only real difference is that when you declare an instance of a primitive data type, memory is set aside according to the length of the type (in bytes), but that memory is not initialised. When declaring an instance of an object, the memory is initialised by virtue of the object's default constructor.

Just as C++ allows primitive data types to be instantiated and initialised in a single assignment statement (int xx=x, for instance), objects can also be initialised by assignment (myObject yy=y) provided a suitable copy constructor exists (myObject yy(y)).

C++ also permits object semantics to be utilised when initialising primitives, such as int xxx(xx). This is merely sugar-coating because primitives have no constructors, however it allows object constructors to initialise primitive members in the same way that they can call specific base class constructors or initialise embedded member objects via the constructor's initialisation list.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

To access members of an object, use the member-of operator. If using a pointer to an object, use the pointer-to-member operator instead. The following minimal example demonstrates both operators:

struct foo

{

int data;

};

int main()

{

foo f; // static reference

foo* p = &f; // pointer to reference

f.data=42; // assign by using member-of operator

p->data=0; // assign by using pointer-to-member operator

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There are several ways to embed one object within another. The four main methods are to embed the object by value, by reference, by pointer and by share. Embedding by reference and by share are essentially the same thing, the only difference being that the share could be NULL (references can never be NULL). By the same token, embedding by value and by pointer are also essentially the same, but again the pointer could be NULL (values, like references, can never be NULL, unless the value happens to be a pointer variable).

The following code demonstrates each of these four different ways of embedding a simple object named foo, and exercises all the constructors, assignment operators and the destructors, using trace stubs to keep track of how each object is instantiated, assigned and ultimately destroyed (most of which occurs automatically when objects fall from scope). Note that the implementation of foo is immaterial. The examples are intended to demonstrate how the containers should be implemented, not what they contain.

You will note that embedding by reference is not ideally suited to containers that have a default constructor (a constructor with no arguments). Ideally, the default constructor should be declared private and preferably disabled altogether. This is because the default constructor must assign something to the embedded reference, and the only way to do so is by instantiating a new embedded object via the constructor. However, this also means the container must maintain an internal flag to denote that it created the object, and subsequently destroy that object when it is itself destroyed. But this would prove disastrous if you copied the container and then destroyed the original, because the copy would then be referencing invalid memory (a dangling reference). Thus to avoid complications, do not use default constructors when embedding references to objects.

Of all the methods presented here, embedding by value is probably the simplest method of all, unless you need to share embedded objects, in which case by share would be the best method. However, if you can live without a default constructor, by reference is easier to implement shares. If you need containers that take ownership of objects that are instantiated externally, then by pointer is the method of choice.

You can, of course, combine these methods into a single class, using a flag to denote whether the object is shared or is allocated internally (remember to use a pointer and not a reference for this). You could also implement your container as a template class, thus allowing any type of object to be embedded.

#include<iostream>

class foo

{

private:

static int s_id; // static data

int m_id; // member data

public:

// default constructor

foo(const foo* p = NULL):m_id(++s_id) {

std::cout<<"creating foo"<<m_id;

if( p )

std::cout<<" from foo"<<p->m_id;

std::cout<<std::endl;

}

// copy constructor

foo(const foo& f):m_id(++s_id) {

std::cout<<"copying foo"<<m_id<<" from foo"<<f.m_id<<std::endl;

}

// destructor

~foo(){

std::cout<<"destroying foo"<<m_id<<std::endl;

}

// assignment operator

foo& operator= (const foo& rhs) {

if( &rhs!=this ) {

std::cout<<"assigning foo"<<m_id<<" from foo"<<rhs.m_id<<std::endl;

}

return( *this );

}

// accessor

const int get_id()const{

return(m_id);

}

};

int foo::s_id=0; // initialise static ID.

class embed_value

{

private:

foo m_val; // embedded member

public:

// default constructor

embed_value(): m_val() {

std::cout<<"creating embed_value from foo"<<m_val.get_id()<<std::endl;

}

// byvalue constructor

embed_value(foo val): m_val(val) {

std::cout<<"creating embed_value from foo"<<get_id()<<std::endl;

}

// copy constructor

embed_value(const embed_value& rhs): m_val(rhs.m_val) {

std::cout<<"copying embed_value containing foo"<<rhs.get_id()<<std::endl;

}

// destructor

~embed_value() {

std::cout<<"destroying embed_value containing foo"<<get_id()<<std::endl;

}

// assignment operator

embed_value& operator= (const embed_value& rhs) {

if( &rhs!=this ) {

std::cout<<"assigning embed_value containing foo"<<get_id()

<<" with embed_value containing foo"<<rhs.get_id()<<std::endl;

m_val=rhs.m_val;

}

return(*this);

}

// accessor

const int get_id()const{

return(m_val.get_id());

}

};

class embed_reference

{

private:

foo& m_ref; // embedded member

// default constructor (must be disabled!)

embed_reference();

public:

// byref constructor

embed_reference(foo& ref): m_ref(ref) {

std::cout<<"creating embed_reference from foo"<<get_id()<<std::endl;

}

// copy constructor

embed_reference(const embed_reference& rhs): m_ref(rhs.m_ref) {

std::cout<<"copying embed_reference containing foo"<<rhs.get_id()<<std::endl;

}

// destructor

~embed_reference() {

std::cout<<"destroying embed_reference containing foo"<<get_id()<<std::endl;

}

// assignment operator

embed_reference& operator= (const embed_reference& rhs) {

if( &rhs!=this ) {

std::cout<<"assigning embed_reference containing foo"<<get_id()

<<" with embed_reference containing foo"<<rhs.get_id()<<std::endl;

m_ref=rhs.m_ref;

}

return(*this);

}

// accessor

const int get_id()const{

return(m_ref.get_id());

}

};

class embed_pointer

{

private:

foo* m_ptr; // embedded member

public:

// default/byptr constructor

embed_pointer(foo* ptr = NULL): m_ptr(ptr?new foo(*ptr):NULL) {

std::cout<<"creating embed_pointer from foo"<<get_id()<<std::endl;

}

// copy constructor

embed_pointer(const embed_pointer& rhs): m_ptr(rhs.m_ptr?new foo(*rhs.m_ptr):NULL) {

std::cout<<"copying embed_pointer containing foo"<<rhs.get_id()<<std::endl;

}

// destructor

~embed_pointer() {

std::cout<<"destroying embed_pointer containing foo"<<get_id()<<std::endl;

delete( m_ptr );

}

// assignment operator

embed_pointer& operator= (const embed_pointer& rhs) {

if( &rhs!=this ) {

std::cout<<"assigning embed_pointer containing foo"<<get_id()

<<" with embed_pointer containing foo"<<rhs.get_id()<<std::endl;

delete( m_ptr );

m_ptr=rhs.m_ptr?new foo(*rhs.m_ptr):NULL;

}

return(*this);

}

// accessor

const int get_id()const{

return(m_ptr?m_ptr->get_id():-1);

}

};

class embed_share

{

private:

foo* m_shr; // embedded member

public:

// default/byshare constructor

embed_share(foo* shr = NULL): m_shr(shr) {

std::cout<<"creating embed_share";

if( shr )

std::cout<<" from foo"<<shr->get_id();

std::cout<<std::endl;

}

// copy constructor

embed_share(const embed_share& rhs): m_shr(rhs.m_shr) {

std::cout<<"copying embed_share containing foo"<<rhs.get_id()<<std::endl;

}

// destructor

~embed_share() {

std::cout<<"destroying embed_share containing foo"<<get_id()<<std::endl;

m_shr=NULL;

}

// assignment operator

embed_share& operator= (const embed_share& rhs) {

if( &rhs!=this ) {

std::cout<<"assigning embed_share containing foo"<<get_id()

<<" with embed_share containing foo"<<rhs.get_id()<<std::endl;

m_shr=rhs.m_shr;

}

return(*this);

}

// accessor

const int get_id()const{

return(m_shr?m_shr->get_id():-1);

}

};

int main()

{

std::cout<<"command: foo foo1;"<<std::endl;

foo foo1;

std::cout<<"\ncommand: embed_value value1;"<<std::endl;

embed_value value1;

std::cout<<"\ncommand: embed_value value2(foo1);"<<std::endl;

embed_value value2(foo1);

std::cout<<"\ncommand: embed_value value3(value2);"<<std::endl;

embed_value value3(value2);

std::cout<<"\ncommand: value3 = value1;"<<std::endl;

value3 = value1;

std::cout<<"\ncommand: embed_reference reference1;"<<std::endl;

embed_reference reference1;

std::cout<<"\ncommand: embed_reference reference2(foo1);"<<std::endl;

embed_reference reference2(foo1);

std::cout<<"\ncommand: embed_reference reference3(reference2);"<<std::endl;

embed_reference reference3(reference2);

std::cout<<"\ncommand: reference3 = reference1;"<<std::endl;

reference3 = reference1;

std::cout<<"\ncommand: embed_pointer pointer1;"<<std::endl;

embed_pointer pointer1;

std::cout<<"\ncommand: embed_pointer pointer2(&foo1);"<<std::endl;

embed_pointer pointer2(&foo1);

std::cout<<"\ncommand: embed_pointer pointer3(pointer2);"<<std::endl;

embed_pointer pointer3(pointer2);

std::cout<<"\ncommand: pointer3 = pointer1;"<<std::endl;

pointer3 = pointer1;

std::cout<<"\ncommand: embed_share share1;"<<std::endl;

embed_share share1;

std::cout<<"\ncommand: embed_share share2(&foo1);"<<std::endl;

embed_share share2(&foo1);

std::cout<<"\ncommand: embed_share share3(share2);"<<std::endl;

embed_share share3(share2);

std::cout<<"\ncommand: share3 = share1;"<<std::endl;

share3 = share1;

std::cout<<"\ncommand: terminate!"<<std::endl;

}

Example output:

===========

command: foo foo1, foo2;

creating foo1

creating foo2

command: embed_value value1;

creating foo3

creating embed_value from foo3

command: embed_value value2(foo1);

copying foo4 from foo1

copying foo5 from foo4

creating embed_value from foo5

destroying foo4

command: embed_value value3(value2);

copying foo6 from foo5

copying embed_value containing foo5

command: value3 = value1;

assigning embed_value containing foo6 with embed_value containing foo3

assigning foo6 from foo3

command: embed_reference reference1(foo1);

creating embed_reference from foo1

command: embed_reference reference2(reference1);

copying embed_reference containing foo1

command: embed_reference reference3(foo2);

creating embed_reference from foo2

command: reference3 = reference1;

assigning embed_reference containing foo2 with embed_reference containing foo1

assigning foo2 from foo1

command: embed_pointer pointer1;

creating embed_pointer from foo-1

command: embed_pointer pointer2(&foo1);

copying foo7 from foo1

creating embed_pointer from foo7

command: embed_pointer pointer3(pointer2);

copying foo8 from foo7

copying embed_pointer containing foo7

command: pointer3 = pointer1;

assigning embed_pointer containing foo8 with embed_pointer containing foo-1

destroying foo8

command: embed_share share1;

creating embed_share

command: embed_share share2(&foo1);

creating embed_share from foo1

command: embed_share share3(share2);

copying embed_share containing foo1

command: share3 = share1;

assigning embed_share containing foo1 with embed_share containing foo-1

command: terminate!

destroying embed_share containing foo-1

destroying embed_share containing foo1

destroying embed_share containing foo-1

destroying embed_pointer containing foo-1

destroying embed_pointer containing foo7

destroying foo7

destroying embed_pointer containing foo-1

destroying embed_reference containing foo2

destroying embed_reference containing foo1

destroying embed_reference containing foo1

destroying embed_value containing foo6

destroying foo6

destroying embed_value containing foo5

destroying foo5

destroying embed_value containing foo3

destroying foo3

destroying foo2

destroying foo1

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Objects are instantiated from class definitions. You access them by their given name just as you access primitive variables by their name. With appropriate operator overloads, you can use objects intuitively just as you would a primitive variable. However, for more complex operations, member methods may be accessed using the member-of operator (.).

Objects can also be instantiated dynamically (on the heap) just as you would a dynamic primitive (using the new operator). To indirectly access the methods you must use the pointer-to-method operator (->). To destroy a dynamic object, you must use the delete operator, just as you would delete a dynamic primitive.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

You're mixing up your terminology. A class is the definition of a type. An object is an instance of that type.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How an object of a class that contain object of other class created in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Is it ever necessary to add extends Object to a class declaration?

No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.


Is there class name as object in java?

Yes, the base class for all other Java classes is Object.


What is class hierarchies in java?

The top level class in Java is class Object. Every other class inherits from Object and therefore Object is the top most in the class hierarchy. If you extend a class from Object such as class Animal and further extend Animal with class Dog then the hierarchy is as follows: Object | Animal | Dog Code for this hierachy is as follows: class Animal { } class Dog extends Animal { } We don't need to write class Animal extends Object because every class extends from Object so it does not need to be stated.


What is topleval class in java?

The top level class in Java is "Object." All other classes are subclasses of Object by default.


Can we create an array of objects for a class having default constructor Justify your answer?

See example code below. #include &lt;iostream&gt; class x { private: // Members. static int counter; int data; public: // Default constructor and destructor. x():data(++counter){printf("Object %d created!\n",data);} ~x(){printf("Object %d destroyed!\n",data);} // other members omitted for brevity... }; // Initialise static counter. int x::counter=0; int main() { // Instantiate an array of 10 objects. x arr[10]; // .. do some work with the array .. return( 0 ); // The array falls from scope, destroying the objects. } Example output: Object 1 created! Object 2 created! Object 3 created! Object 4 created! Object 5 created! Object 6 created! Object 7 created! Object 8 created! Object 9 created! Object 10 created! Object 10 destroyed! Object 9 destroyed! Object 8 destroyed! Object 7 destroyed! Object 6 destroyed! Object 5 destroyed! Object 4 destroyed! Object 3 destroyed! Object 2 destroyed! Object 1 destroyed!

Related questions

What is object class in java?

object class is a super class for all other class...


Which of the follwing items is a valid leaf object in Active Directory?

it is the user because you can not expand itA leaf object cannot contain other objects and usually refers to a resource such as a printer, folder, user or group UserDomain, user, printer ,ou folder or siteObjects are either container objects or leaf objects. A container object stores other objects and it occupies a specific level in a subtree hierarchy.An object class is a container if at least one other class specifies it as a possible superior; therfore any object class defined in the schema can become a container. A leaf objectdoes not store other objects, and, as such, it occupies the endpoint of a subtreea directory service is essentially a container that can contain other containers. This is certainly true of AD. Objects in Active Directory can be either containers for other objects or they can be leaf objects, which do not serve as containers. A user object is an example of a leaf object because it stores user data but does not contain other objects.


Is it ever necessary to add extends Object to a class declaration?

No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.No; that is the default. In other words, if you don't add "extends", the class will automatically inherit from the "Object" class.


Is there class name as object in java?

Yes, the base class for all other Java classes is Object.


What is class hierarchies in java?

The top level class in Java is class Object. Every other class inherits from Object and therefore Object is the top most in the class hierarchy. If you extend a class from Object such as class Animal and further extend Animal with class Dog then the hierarchy is as follows: Object | Animal | Dog Code for this hierachy is as follows: class Animal { } class Dog extends Animal { } We don't need to write class Animal extends Object because every class extends from Object so it does not need to be stated.


What is topleval class in java?

The top level class in Java is "Object." All other classes are subclasses of Object by default.


Is the word classes an object?

Classes is the plural of the word class. A class is not an object in the physical sense, it is an idea, a notion or concept. We typically use a class to define a classification, such as a class of people, a class of study or a class of object. In other words it defines a type.


What is hierarchy in java?

The top level class in Java is class Object. Every other class inherits from Object and therefore Object is the top most in the class hierarchy. If you extend a class from Object such as class Animal and further extend Animal with class Dog then the hierarchy is as follows: Object | Animal | Dog Code for this hierachy is as follows: class Animal { } class Dog extends Animal { } We don't need to write class Animal extends Object because every class extends from Object so it does not need to be stated.


Can we create an array of objects for a class having default constructor Justify your answer?

See example code below. #include &lt;iostream&gt; class x { private: // Members. static int counter; int data; public: // Default constructor and destructor. x():data(++counter){printf("Object %d created!\n",data);} ~x(){printf("Object %d destroyed!\n",data);} // other members omitted for brevity... }; // Initialise static counter. int x::counter=0; int main() { // Instantiate an array of 10 objects. x arr[10]; // .. do some work with the array .. return( 0 ); // The array falls from scope, destroying the objects. } Example output: Object 1 created! Object 2 created! Object 3 created! Object 4 created! Object 5 created! Object 6 created! Object 7 created! Object 8 created! Object 9 created! Object 10 created! Object 10 destroyed! Object 9 destroyed! Object 8 destroyed! Object 7 destroyed! Object 6 destroyed! Object 5 destroyed! Object 4 destroyed! Object 3 destroyed! Object 2 destroyed! Object 1 destroyed!


What are Instance variables java?

Instance variableA variable, part of an Object. These might better be called perObject variables since each instantiated object of this class will have its own private copy of this variable. They are allocated when the object is allocated via new. Static methods may not access the instance variables of their class (or any other class for that matter), other that via some object reference, e.g. anObject.someField. Static methods may even access private instance variables in their class via some object reference.


What must a neutral object contain?

The electrical charges were each other neutralized.


Objects in c plus plus and how are they created?

An object is a component of a program that knows how to perform certain actions and to interact with other pieces of the program.it is created as following...... An object is an instance of a class. For object read variable! Every time we define a variable we get a new object which has all the properties of the associated class. This is the same as with the built-in types of C (float, int etc.)-they have certain properties (range of values, operations etc), the bonus is that we can make up.