How are an object’s data members initialized if a class has only an implicitly defined default constructor?
And why a class might provide a "set" function and a "get" function for a data member?
What is the purpose of the scope resolution operator?

>>How are an object’s data members initialized if a class has only an implicitly defined default constructor
They aren't initialized -- they contain whatever value happens to be there at the time the class object is instantiated. That's the purpose of writing your own constructor.

>>And why a class might provide a "set" function and a "get" function for a data member?
Its really a matter of coding style and good OOP. Class variables should only be changed by functions/methods within the class and not allow others to change them. So make variables either protected or private the write put and get methods to provide appropriate access. This also has the advantage of insuring the class variables are changed in only one place -- if you want to change the class variable you only have to change it in one place instead of all other the program whereever its used. Makes debugging and maintenance a lot easier.

>And why a class might provide a "set" function and a "get" function for a data member?
If you wisely give your data members only private access, then the only way to access them from outside of the function they're declared in is to use public or protected member functions. Making data members private is wise because you have complete control over them in a localized fashion. Public and protected members are not much better than global variables when it comes to potential abuse and corruption.

>What is the purpose of the scope resolution operator?
With standard C++ you can place your classes and functions in separate namespaces. However, once you do that, how can you specify which namespace you want to use?

void baz() {}

namespace Foo {
  void baz() {}
}

namespace Bar {
  void baz() {}
}

The answer is the scope resolution operator:

::baz(); // Use the global baz
Foo::baz(); // Use Foo's baz
Bar::baz(); // Use Bar's baz

>> How are an object’s data members initialized if a class has only an implicitly defined default constructor?
if every non-static member variable of a class has default initialization and no programmer defined constructor is specified, a default constructor is deemed to be declared.

if the initialization of all the non-static member variables are trivial, then the default constructor is also trivial and will not be synthesized by the implementation. for example,

struct A 
{ 
   int i ; 
   double d ; 
   char* ptr ; 
} ;

a default constructor is deemed to be declared, but no constructor is defined and no initialization is performed.

if there are non-static member variables with non-trivial default initialization, then a default constructor is synthesized by the implementation. for example,

struct B 
{ 
   std::string str ; 
   std::vector<int> container ; 
   int i ; 
};

a default constructor is defined by the implementation. the synthesized default constructor will initialize the members str and container by calling their default constructors, for member i, no initialization is performed.

for

struct C
{ 
   const int i ; 
   double d ; 
   char* ptr ; 
} ;

no default constructor is deemed to be declared. (member i has no default initialization.)

note: a. whatever was said of non-static member variables also applies to non-virtual base classes.
b. for classes with virtual functions and/or virtual base classes, the hidden vtable pointer and vbase pointer members have non-trivial initialization.
c. the non-programmer defined default constructor is deemed to be public and inline.

for a good discussion of these issues, see stan lippman's 'inside the c++ object model' http://safari.awprofessional.com/0201834545

commented: good compilete answer +18

i was intrigued to read this book :: 'inside the c++ object model' but then i saw that it was published in 1996.... isn't outdated? i mean we have the {i am not so sure about the dates} 99, 2001 and c0x specification of c++, the object model hasn't changed?

>> i saw that it was published in 1996.... isn't outdated? i mean we have the {i am not so sure about the dates} 99, 2001 and c0x specification of c++, the object model hasn't changed?
it has not changed as far as the fundamentals of construction, destruction, copy, virtual functions, inheritance, object layout etc. are concerned. some grey areas have been clarified (eg. rv and nrv optimization, optimizing away anonymous temporaries etc.). and the book is clearly dated in discussing generic programming (templates) and exceptions. most (almost all) of the additions in c++0x are in the standard library; not the core language. it does not really change anything that is already in place.

ah, i see...so when i finish with the exams, i will get a hold of it...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.