?
Solved

Could someone help me initialse the class constructor

Posted on 2013-05-14
4
Medium Priority
?
396 Views
Last Modified: 2013-05-19
ClassA{
public:
        ClassA(int kg);
      virtual ~ClassA();
private:
      int kk;
};
ClassA::ClassA(int kg) {
      kk = kg;
}

ClassA::~ClassA() {
}
class classB {
public:
      classB();
      virtual ~classB();  
        ClassA ca;
};

classB::classB()//I am getting error at this line
{
     //how to initialse the constructor of B
}

classB::~classB() {

}
0
Comment
Question by:pvinodp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 1200 total points
ID: 39164724
Hi pvinodp,

the problem is the constructor of ClassA expects an int as argument, but the way it's implemented it can only be used when there's a default constructor. You can either add a default constructor to ClassA or change the existing constructorto take a default argument or initialize 'ca' in classB's constructor using an initialization list, i.e.:
// method 1: implement a default constructor
class ClassA
    ClassA(int kg=0); // added default argument so this becomes a default constructor
    virtual ~ClassA();
private:
      int kk;
};

ClassA::ClassA(int kg) {
      kk = kg;
}

// method 2: initialize 'ca'
classB::classB()
: ca( 5 ) // using 5 for just as sample
{
}

Open in new window

Hope this helps,

ZOPPO
0
 
LVL 32

Accepted Solution

by:
phoffric earned 800 total points
ID: 39165743
Sometimes you are not able to modify ClassA as it may be written by a third party. But if you can modify classB, then you could take a couple of approaches. One approach is to provide a default value for classA in the classB constructor initialization list (as Zoppo as shown). Another approach is to add another classB constructor that takes an arg that can be used for the ca initialization. Both approaches are shown below with no functional change to ClassA.
#include <iostream>
using namespace std;

class ClassA {
public:
      ClassA(int kg) : kk(kg) {}
      virtual ~ClassA() {}
      int get_kk() {return kk;}
private:
      int kk;
};


class classB {
public:
      classB()  : ca(12) {}
      classB(int Bkg) : ca(Bkg) {} // add constructor to provide value for ClassA
      virtual ~classB() {}  
      ClassA ca;
};


int main() {
   ClassA myA(10);
   classB myClassB;
   classB myClassB2(22);

   cout << myA.get_kk() << endl;
   cout << myClassB.ca.get_kk() << endl;
   cout << myClassB2.ca.get_kk() << endl;
}

Open in new window

0
 

Author Closing Comment

by:pvinodp
ID: 39167169
thanks for your answers
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question