Thread: constructor purpose?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    constructor purpose?

    if i have
    Code:
    //blah.h
    class operation {
    public:
      operation();
      virtual int performCalculation(int a, int b);
    };
    
    //and then i have all the different types of operations that i can do like plus, minus, multiply
    
    class plus: public operation {
    public:
      int performCalculation(int a, int b);
    };


    now why is it that im getting errors
    [code]

    blah.h:12: error: base `operation' with only non-default constructor in class
    without a constructor

    and line 12 is the line
    Code:
    class plus: public operation {
    can i not have a constructor for my plus class? or is it better to have one? and even if i do make one whats the purpose when all i want to do is just a+b .. whats a constructor going to do ?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try removing your constructor that you declared in your base class operation and see what happens.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    You don't have to supply a constructor. A default constructor is called if you don't specify one. A constructor is used to initialize values when the class object is created. So if you create a class called square with members for length and width you can initialze them both to whatever values you need.
    Code:
    class square{
    public:
             int length;
             int width;
    //constructor definition
             square (int L,int W)   
                  {
                   length=L;
                   width=W;  
                  }
    };
    
    square mysquare(12,10);  creates and intializes length and width;
    Now that you have created a constructor what happesn if you create an instance without initialzing
    Code:
    square mysquare();
    it assumes that you are going to take care of everything so it looks for a default constructor that you provide. A simple default constructor is just empty brackets

    Code:
    //default constructor
    square()
    {
    }
    Last edited by manofsteel972; 08-17-2004 at 05:22 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    98
    blah.h:12: error: base `operation' with only non-default constructor in class
    without a constructor
    I think this non-default constructor imply class plus defined without one constructor function, maybe changelike this:
    Code:
    class plus: public operation 
    {
        public:
        int performCalculation(int a, int b);  //this statement ??
        plus();          //add this , but what type define it?
        
    };
    I just learn class, and cant understand deeply,
    I think class & constructor is most difficultly for newbie like me.
    sth wrong writing told me please.
    thanks.
    Last edited by toysoldier; 08-17-2004 at 05:44 AM.

Popular pages Recent additions subscribe to a feed