inheritance means aquiring the properties of the other object
i.e for example
class first
{
int a;
first()
{
a= 10;
}
}
class second : public first
{
int b;
public :
second()
{
b=20;
}
void display()
{
cout%26lt;%26lt;"the value of a is "%26lt;%26lt;a ;
cout%26lt;%26lt;"the value of b is %26lt;%26lt;b;
}
}
void main()
{
second sec;
sec.display();
}
here in the example i took two class with name first and second
and i derived the second class from the first class then i created a object and called the second class function display(). though a is not the variable of second display function will display the value of the a. this is the simple example of inheritance
we can controll the inheritance by giving the access specifiers before the derived class i.e
syntax:
CLASS derivedclass:ACCESS SPECIFIER another class
whereACCESS SPECIFIER can be PUBLIC, PRIVATE, PROTECTED
if u want more about this make a mail to me
now ur second question answer is as follows
polymorphism means that some code or operations or objects behave differently in different contexts.there are 2 types of polymorphism
1. compile time polymorphism
2. runtime polymorphism
i am giving a example of compile time polymorphism(function overloading)
void main()
{
void fun();
void fun(10);
void fun(10,20);
}
void fun()
{
cout%26lt;%26lt;" i am in the function with no aruguments";
}
void big(int a)
{
cout%26lt;%26lt;" i am in the function with one arugument";
}
void big(int a,int b)
{
cout%26lt;%26lt;" i am in the function with 2 aruguments";
}
here in this example i took a set of functions having no aruguments ,with one arugment and 2 aruguments
depending on the function calls the appropirate functions will be called
Can somebody give me an example on polymorphism and inheritance in C++?:)?
Suppose you wanted to write a class called door. You would declare it like this:
class Door
{
public:
virtual void open();
virtual void close();
}
Now you want to write a GlassDoor class:
Class GlassDoor : public Door
{
public:
void open ()
{
// implementation of glass door's open function, how to open a glass door.
}
void close ()
{
// implementation of glass door's close function, how to close a glass door.
}
}
As you look at the simple structures you will see class Door is an abstract class of all Door in this world. Assuming that all doors in the world can only perform 2 actions: open and close.
This 2 method of open and close generalize the characteristic of all doors without specificly explain HOW.
This is called POLYMORPHISM.
The GlassDoor class which inherited from Door class MUST implement the 2 functions open() and close(). Later on, you would create a house with many different type of door, to open any door, you would only call open(), and to close, call close() without the need to know which kind of door it was. This concept called INHERITANCE.
I left C++ for a while now, so my C++ syntax is a little off, but the concept is the same for all languages.
For better studying on OOP concept, you can check out here:
http://en.wikipedia.org/wiki/C++
hope this help
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment