classes

tell me about classes in c++ using object oriented approch

Page 1 of 1

7 Replies - 796 Views - Last Post: 29 August 2008 - 04:33 AM Rate Topic: -----

#1 nosheen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 20-August 08

classes

Posted 28 August 2008 - 12:05 AM

tell me about classes how can inherit a class single time and multiple time
in c++ using object oriented approch
Is This A Good Question/Topic? 0
  • +

Replies To: classes

#2 sensui  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: classes

Posted 28 August 2008 - 12:27 AM

How to inherit a single class:
class BaseClass {
  private:
    //member declarations
  public:
    BaseClass() { };  //constructor
    ~BaseClass() { }; //destructor
    //method declarations
};

class DerivedClass: public BaseClass {
  private:
    //new member declarations
  public:
  //note that the DerivedClass constructor
  //must call the BaseClass constructor before
  //it executes his code
    DerivedClass() : BaseClass() { };
    ~DerivedClass() { };
    //new method declarations
};

Multiple class inheritance:
class FirstBaseClass {
  private:
    //member declarations
  public:
    FirstBaseClass() { };  //constructor
    ~FirstBaseClass() { }; //destructor
    //method declarations
};

class SecondBaseClass {
  private:
    //member declarations
  public:
    SecondBaseClass() { };  //constructor
    ~SecondBaseClass() { }; //destructor
    //method declarations
};

class DerivedClass: public FirstBaseClass, public SecondBaseClass {
  private:
    //new member declarations
  public:
    DerivedClass() { };
    ~DerivedClass() { };
    //new method declarations
};



The public inheritance is the most used, but there are two more types of inheritance: protected and private. You can find more information here.

Was this post helpful ? :) -->

This post has been edited by sensui: 28 August 2008 - 12:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: classes

Posted 28 August 2008 - 07:41 AM

You don't have to explicitly call the base class constructor. It will do it for you. As well as call the destructor.
Was This Post Helpful? 0
  • +
  • -

#4 sensui  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: classes

Posted 28 August 2008 - 07:52 AM

I agree with you KYA, but this works only with the default constructor of the base class. If you have a constructor for the base class with arguments how do you call it from the constructor of the derived class?
Example:
class BaseClass {
  private:
    //member declarations
    int m_int;
  public:
    BaseClass() { };  //default constructor
    BaseClass( int myVariable ) { m_int = myVariable }; //another constructor
    ~BaseClass() { }; //destructor
    //method declarations
};

class DerivedClass: public BaseClass {
  private:
    //new member declarations
  public:
  //note that the DerivedClass constructor
  //must call the BaseClass constructor before
  //it executes his code
  //example: for a constructor other than the default
    DerivedClass( int myInt) : BaseClass( myInt ) { };
    ~DerivedClass() { };
    //new method declarations
};

This post has been edited by sensui: 28 August 2008 - 07:52 AM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: classes

Posted 28 August 2008 - 08:21 AM

Why would you want to? Inherited classes need to have their own constructor and destructor.
Was This Post Helpful? 0
  • +
  • -

#6 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Re: classes

Posted 28 August 2008 - 05:34 PM

Here is the explanation for your question sensui: All inherited constructors are called when creating a new object, and all destructors are called when destroying an object.

// Calling multiple constructors
#include <iostream>
using namespace std;

     typedef int HANDS;
     enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ;
     enum BOOL { FALSE, TRUE };

     class Horse
     {
    public:
       Horse(COLOR color, HANDS height);
       virtual ~Horse() { cout << "Horse destructor...\n"; }
       virtual void Whinny()const { cout << "Whinny!... "; }
       virtual HANDS GetHeight() const { return itsHeight; }
       virtual COLOR GetColor() const { return itsColor; }
    private:
       HANDS itsHeight;
       COLOR itsColor;
    };

    Horse::Horse(COLOR color, HANDS height):
       itsColor(color),itsHeight(height)
    {
       cout << "Horse constructor...\n";
    }

    class Bird
    {
    public:
       Bird(COLOR color, BOOL migrates);
       virtual ~Bird() {cout << "Bird destructor...\n";  }
       virtual void Chirp()const { cout << "Chirp... ";  }
       virtual void Fly()const 
       { 
          cout << "I can fly! I can fly! I can fly! "; 
       }
       virtual COLOR GetColor()const { return itsColor; }
       virtual BOOL GetMigration() const { return itsMigration; }
 
    private:
       COLOR itsColor;
       BOOL itsMigration;
    };

    Bird::Bird(COLOR color, BOOL migrates):
       itsColor(color), itsMigration(migrates)
    {
       cout << "Bird constructor...\n";
    }

    class Pegasus : public Horse, public Bird
    {
    public:
       void Chirp()const { Whinny(); }
       Pegasus(COLOR, HANDS, BOOL,long);
       ~Pegasus() {cout << "Pegasus destructor...\n";}
       virtual long GetNumberBelievers() const 
        { 
           return  itsNumberBelievers; 
        }

    private:
       long itsNumberBelievers;
    };
 
    Pegasus::Pegasus(
        COLOR aColor, 
        HANDS height, 
        BOOL migrates, 
        long NumBelieve):
    Horse(aColor, height),
    Bird(aColor, migrates),
    itsNumberBelievers(NumBelieve)    
    {
       cout << "Pegasus constructor...\n";
    }

    int main()
    {
       Pegasus *pPeg = new Pegasus(Red, 5, TRUE, 10);
       pPeg->Fly();
       pPeg->Whinny();
       cout << "\nYour Pegasus is " << pPeg->GetHeight();
       cout << " hands tall and ";
       if (pPeg->GetMigration())
          cout << "it does migrate.";
       else
          cout << "it does not migrate.";
       cout << "\nA total of " << pPeg->GetNumberBelievers();
       cout << " people believe it exists.\n";
       delete pPeg;
     return 0;
    }

 


Here is the output:
Horse constructor...
Bird constructor...
Pegasus constructor...
I can fly! I can fly! I can fly! Whinny!...
Your Pegasus is 5 hands tall and it does migrate.
A total of 10 people believe it exists.
Pegasus destructor...
Bird destructor...
Horse destructor...

An also before I forget you have to pass as arguments the Bird and Horse constructors to pegasus, look us this lines:
 Pegasus::Pegasus(
        COLOR aColor, 
        HANDS height, 
        BOOL migrates, 
        long NumBelieve):
    Horse(aColor, height), //horse constructor as parameter
    Bird(aColor, migrates), //bird constructor as parameter
    itsNumberBelievers(NumBelieve)    
    {
       cout << "Pegasus constructor...\n";
    }

This post has been edited by nirvanarupali: 28 August 2008 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: classes

Posted 28 August 2008 - 07:32 PM

Also note in nirvanarupali's code that the destructors of the base class are declared virtual, which ensures they are called when the derived classes are deleted.
Was This Post Helpful? 0
  • +
  • -

#8 sensui  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 30
  • View blog
  • Posts: 146
  • Joined: 24-August 08

Re: classes

Posted 29 August 2008 - 04:33 AM

@nirvanarupali: that's exactly what I said in my previous post ( line 20 ):
DerivedClass( int myInt) : BaseClass( myInt ) { };
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1