Inheritance!!!
-
Please see the code bellow.
#include <iostream>
#include <stdlib.h>using namespace std;
class Cpolygon
{
protected:
int width, height;
public:
void setvalues (int a, int b)
{
width=a;
height=b;
}
};class Crectangle:public Cpolygon
{
public:
int area ()
{
return(width*height);
}
};class Ctriagle:public Cpolygon
{
public:
int area()
{
return((width*height)/2);
}
};int main(int argc, char *argv[])
{Crectangle rect;
Ctriagle tril;Cpolygon *Poly1 =▭
Cpolygon *Poly2 =&tril;Poly1->setvalues(4,5);
Poly2->setvalues(4,5);cout<<Poly1->area()<<endl;
cout<<Poly2->area()<<endl;system("PAUSE");
return 0;
}I have two questions. 1.
Cpolygon *Poly1 =▭
Cpolygon *Poly2 =&tril;What does these two lines indicate? 2. Instead of creating two objects of base class can we use only one object of base class to set the values. i.e.
Cpolygon *Poly;
So that
Poly->setvalues(4,5);
need to call only once and create the objects of derived class and show the outputs i.e.
Crectangle *rect;
Ctriagle *tril;cout<<rect->area()<<endl;
cout<<tril->area()<<endl;will it work? :confused: Thanks for reading.
-
Please see the code bellow.
#include <iostream>
#include <stdlib.h>using namespace std;
class Cpolygon
{
protected:
int width, height;
public:
void setvalues (int a, int b)
{
width=a;
height=b;
}
};class Crectangle:public Cpolygon
{
public:
int area ()
{
return(width*height);
}
};class Ctriagle:public Cpolygon
{
public:
int area()
{
return((width*height)/2);
}
};int main(int argc, char *argv[])
{Crectangle rect;
Ctriagle tril;Cpolygon *Poly1 =▭
Cpolygon *Poly2 =&tril;Poly1->setvalues(4,5);
Poly2->setvalues(4,5);cout<<Poly1->area()<<endl;
cout<<Poly2->area()<<endl;system("PAUSE");
return 0;
}I have two questions. 1.
Cpolygon *Poly1 =▭
Cpolygon *Poly2 =&tril;What does these two lines indicate? 2. Instead of creating two objects of base class can we use only one object of base class to set the values. i.e.
Cpolygon *Poly;
So that
Poly->setvalues(4,5);
need to call only once and create the objects of derived class and show the outputs i.e.
Crectangle *rect;
Ctriagle *tril;cout<<rect->area()<<endl;
cout<<tril->area()<<endl;will it work? :confused: Thanks for reading.
Before answering your question, I think there should be a small modification in your code of the Cpolygon class: you should have a virtual area function:
class Cpolygon
{
...
virtual int area() = 0;
};Otherwise those two lines won't compile:
cout<<Poly1->area()<<endl;
cout<<Poly2->area()<<endl;First question: you have two pointers to a base class and you make them point to derived classes. This would be more or less similar as this:
Cpolygon *Poly1 = new Crectangle();
Cpolygon *Poly2 =new Ctriangle();Which is seen more often. In fact the whole purpose of this code is to show you the use of polymorphism (I suggest you google for it to have a lot more examples). The base principle is that you can manipulate objects of different types (Crectangle and Ctriangle) exactly the same way: they all implement a known interface (Cpolygon). This way, you can store them all in a container and you don't need to know which exact type you are manipulating. Each time you call a virtual function (like area()), it will be "redirected" to the correct type. This is one of the most basic and fundamental principle of object oriented programming.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Please see the code bellow.
#include <iostream>
#include <stdlib.h>using namespace std;
class Cpolygon
{
protected:
int width, height;
public:
void setvalues (int a, int b)
{
width=a;
height=b;
}
};class Crectangle:public Cpolygon
{
public:
int area ()
{
return(width*height);
}
};class Ctriagle:public Cpolygon
{
public:
int area()
{
return((width*height)/2);
}
};int main(int argc, char *argv[])
{Crectangle rect;
Ctriagle tril;Cpolygon *Poly1 =▭
Cpolygon *Poly2 =&tril;Poly1->setvalues(4,5);
Poly2->setvalues(4,5);cout<<Poly1->area()<<endl;
cout<<Poly2->area()<<endl;system("PAUSE");
return 0;
}I have two questions. 1.
Cpolygon *Poly1 =▭
Cpolygon *Poly2 =&tril;What does these two lines indicate? 2. Instead of creating two objects of base class can we use only one object of base class to set the values. i.e.
Cpolygon *Poly;
So that
Poly->setvalues(4,5);
need to call only once and create the objects of derived class and show the outputs i.e.
Crectangle *rect;
Ctriagle *tril;cout<<rect->area()<<endl;
cout<<tril->area()<<endl;will it work? :confused: Thanks for reading.
A new comers' reply: For question 1: Try the step below: Add "virtual int area() =0;"(of course it is a public member) in the definition of class Cpolygon. For the lesson, search virtual class on the Internet. For question 2: I admire your imagination. It is possible, but not in your way. See the use of "constructor". Especially "default constructor."
With ears and eyes.