Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,175 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,198 people online right now. Registration is fast and FREE... Join Now!




classes

 
Reply to this topicStart new topic

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

nosheen
27 Aug, 2008 - 11:05 PM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2008
Posts: 4

tell me about classes how can inherit a class single time and multiple time
in c++ using object oriented approch

User is offlineProfile CardPM
+Quote Post

sensui
RE: Classes
27 Aug, 2008 - 11:27 PM
Post #2

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
How to inherit a single class:
cpp
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:
cpp
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 ? smile.gif -->

This post has been edited by sensui: 27 Aug, 2008 - 11:42 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Classes
28 Aug, 2008 - 06:41 AM
Post #3

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
You don't have to explicitly call the base class constructor. It will do it for you. As well as call the destructor.
User is offlineProfile CardPM
+Quote Post

sensui
RE: Classes
28 Aug, 2008 - 06:52 AM
Post #4

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
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:
cpp
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 Aug, 2008 - 06:52 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Classes
28 Aug, 2008 - 07:21 AM
Post #5

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
Why would you want to? Inherited classes need to have their own constructor and destructor.
User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: Classes
28 Aug, 2008 - 04:34 PM
Post #6

D.I.C Stomach
Group Icon

Joined: 1 Aug, 2007
Posts: 995



Thanked: 4 times
Dream Kudos: 375
My Contributions
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.

cpp
// 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:
cpp
 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 Aug, 2008 - 04:38 PM
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Classes
28 Aug, 2008 - 06:32 PM
Post #7

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 862



Thanked: 89 times
Dream Kudos: 50
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

sensui
RE: Classes
29 Aug, 2008 - 03:33 AM
Post #8

D.I.C Head
Group Icon

Joined: 24 Aug, 2008
Posts: 132



Thanked: 20 times
Dream Kudos: 50
My Contributions
@nirvanarupali: that's exactly what I said in my previous post ( line 20 ):
DerivedClass( int myInt) : BaseClass( myInt ) { };
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 03:34AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month