By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Object Creation Problem

P: 34
Hello All,
I was going through a very simple looking program, but as I am a newbie in C++ it created problem for me. I thought it will give compilation error but it went smoothly.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class sample
  5. {
  6.     int i;
  7.     public:
  8.         sample(int ii)
  9.         {
  10.             i=ii;
  11.         }
  12. };
  13.  
  14. class comp
  15. {
  16.       sample s;
  17.     public:
  18.         comp(int ii) : s(10)
  19.         {
  20.         }
  21. };
  22.  
  23.  
  24. int main()
  25. {
  26.     comp c(10);
  27. }
I faced following problems:
(1) In class comp object of class sample has been created but no corresponding constructor is in sample class,i.e., constructor with zero arguments, so this should create an error, but it doesn't.
(2) What is the meaning of : operator in comp(int ii) : s(10).

Please help me
thanx in advance.
Jun 2 '07 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
I moved this thread from the C/C++ Articles to this forum.

(1) In class comp object of class sample has been created but no corresponding constructor is in sample class,i.e., constructor with zero arguments, so this should create an error, but it doesn't.
A constructor is used to initialize objects when they are created. If a class has no constructor, then then class members will be initialized according to their type.

In your case, Sample does have a constructor. Because of this, all Sample objects will require a constructor:

Expand|Select|Wrap|Line Numbers
  1. Sample obj(25);   //OK calls the Sample constructor
  2.  
  3. Sample obj1;      //ERROR: no appropriate constructor
  4.  
The : operator identifies an initializer list. The initializer list is used to place values into the class members before the { of the constructor. The comp class has a Sample member. The Sample member will created and initialized before the { of the comp constructor. In your example, this member is initialized by calling the Sample constructor with a 10.

If you remove the initializer list:

Expand|Select|Wrap|Line Numbers
  1. comp(int ii) 
  2.         {
  3.         }
  4.  
then the Sample member will still need to be initialized but the Sample class does not have a default constructor. As a result, your compiler will produce an error.
Jun 2 '07 #2

P: 2
(2) What is the meaning of : operator in comp(int ii) : s(10), The : operator of a constructor is a initializer of a member variable or a base class, that is member variable s is initialized (it's constructor called) with a value of 10.

For instance
Expand|Select|Wrap|Line Numbers
  1.  class MyC
  2. {
  3. public:
  4.   int i1, i2;
  5.   MyC( int x, int y) : i1(x), i2(y) {}
  6. };
  7.  
initialized member variable i1 with x and i2 with y. If base class with constructor then this will initialize base class.

Expand|Select|Wrap|Line Numbers
  1.  class MyD : public MyC
  2.   public:
  3.  MyD( int z ) : MyC( z, z+1 ) {}
  4. };
  5. MyD d(3);
  6.  
d.i1 will equal 3 and d.i2 will equal 4.



Hello All,
I was going through a very simple looking program, but as I am a newbie in C++ it created problem for me. I thought it will give compilation error but it went smoothly.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class sample
  5. {
  6.     int i;
  7.     public:
  8.         sample(int ii)
  9.         {
  10.             i=ii;
  11.         }
  12. };
  13.  
  14. class comp
  15. {
  16.       sample s;
  17.     public:
  18.         comp(int ii) : s(10)
  19.         {
  20.         }
  21. };
  22.  
  23.  
  24. int main()
  25. {
  26.     comp c(10);
  27. }
I faced following problems:
(1) In class comp object of class sample has been created but no corresponding constructor is in sample class,i.e., constructor with zero arguments, so this should create an error, but it doesn't.
(2) What is the meaning of : operator in comp(int ii) : s(10).

Please help me
thanx in advance.
Jun 2 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.