Polymorphism

Sep 12, 2014 at 5:18pm
Hey guys i used polymorphism and Inheritance but still i van not find any i=usage except that it avoids code duplication .... what else could be the use of polymorphism and inheritance plz explain me ..... i'll be thankfull to you....
plz help.....

Sep 12, 2014 at 6:26pm
I'm no expert so it'd be good to get a second opinion, but I think a lot of these higher level concepts exist just to avoid code duplication. By that, I mean that polymorphism and inheritance is never necessary. One could always achieve the same thing by writing a large block of simpler code.

Functions are a great example of the same principle. Let's say you have a need to select the greater of two numbers:

1
2
3
4
5
6
7
8
9
10
11
int selectgreater(int a, int b)
{
if( a>b )
{
return a;
}
else
{
return b;
}
}


You could write that in your code every time you wanted to use it, but we use functions to avoid unnecessary duplication of the code.
Sep 12, 2014 at 6:32pm
It's not about code duplication, it's about not needing to know the actual type of an object to have it do what you want.
Sep 12, 2014 at 6:58pm
It's not about code duplication, it's about not needing to know the actual type of an object to have it do what you want.


Not OP, but I'm not sure I get this and I want to understand. By creating an object of a certain derived class, don't you already know which parent classes it belongs to?
Sep 12, 2014 at 7:25pm
1
2
3
void f(Base &b)
{
}
Do you know the actual type of "b"? Is it MyDerived? Is it TheirDerived? It doesn't matter.
Sep 12, 2014 at 8:17pm
Ah, I see what you mean. Thanks for the example.
Sep 13, 2014 at 3:18pm
LB plz give me a clear answer .... im not getting it .....
Sep 13, 2014 at 3:51pm
For example let there be Person class and there is a function which reads data from stream:
1
2
3
4
5
6
Person read_person(std::istream& in)
{
    Person p;
    in >> p.name >> p.surname >> p.age;
    return p;
}
It takes a reference to istream. One example of istream object is std::cin: standard input.
There is ifstream class, which is derived from istream. Thanks to inheritance we can do following:
1
2
3
4
5
Person x, y;
x = read_person(std::cin); //read data from console

std::ifstream input("persondata.txt");
y = read_person(input); //read data from a file 
Note that our read_person function does not know about file streams existense at all, but can use them. If we want, we can create class network_stream derived from istream and it would work with that function too.
Sep 13, 2014 at 6:32pm
I've noticed my usage of classes differs slightly from yours. Is there any significant difference between what you did, and what I typically do? Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using namespace std;

class Person
{

string name, surname;
int age;

Person();
void read_person(std::istream& in);
};

Person p;

Person::Person()
{}

void Person::read_person(std::istream& in)
{
in >> name >> surname >> age;
return;
}


Would our two codes achieve the same thing? I've never myself declared a class within a class like you did with:

1
2
3
4
5

Person read_person(std::istream& in)
{
Person p;
}


Thanks! (btw, how do I indent lines in code mode here?)
Sep 14, 2014 at 4:33am
btw, how do I indent lines in code mode here?
Spaces. Or copypaste already idented code from your favorite editor.

In my code read_person is not a class member, it is declared as standalone function to decrease class dependencies. Your code would work to and it does the same thing but has slightly different interface:
1
2
3
4
//My usage:
x = read_person(cin);
//Your
x.read_person(cin);
Both are fine.
Sep 14, 2014 at 4:46am
Oh, I see. I automatically read that as Person::read_person, but obviously it would still have to have a return type. Thanks for the info!
Topic archived. No new replies allowed.