### Understanding operators

So I can't understand how this works:
Here are 3 classes:
1.
 ``12345678910111213141516`` ``````class L { public: int t; L() { t = 0; } virtual ~L() { cout << ++t << " "; } void i() { cout << t + 5 << " "; } virtual void g() { cout << t++ << " "; } }; ``````

2.
 ``123456789101112131415`` ``````class O : public L { public: O() { t = 7; } virtual ~O() { cout << --t << " "; } virtual void i() { cout << ++t << " "; } virtual void g() { cout << t++ << " "; } };``````

3.
 ``123456789101112131415`` ``````class R : public L { public: R() { t = 1; } virtual ~R() { cout << t++ << " "; } void i() { cout << ++t << " "; } void g() { cout << t-- << " "; } };``````

What will be shown after did these functions below?
 ``123456789101112131415161718`` ``````1.1. void test1() { L l; } 1.2. void test2() { R* r = new R(); delete r; } 1.3. void test3() { R b; b.i(); L& a = b; a.g(); } 1.4. void test4() { O b; L& a = b; a.i(); } ``````

Could you explain each how It works?
For example what means L l;? and etc.
Last edited on
closed account (j3Rz8vqX)
Exactly what ryan said, but to be specific to your last question:
 For example what means L l;?

1.1) L l is simply creating a object of type "L" that would be called with "l"; a basic class object.

The link above will provide info on polymorphism, but you can think of it as:

Assuming the base as: Shape.

Some polymorphs of shape are: Circle, triangle, and square.

They are all abstracts of shape, having everything shape has plus a bit more; possibly specific to each shape individually.

It can be restrictions, additives, modifications, ect..

In this case, possibly constant number of sides, sum of angles, uniquely available formulas, different child classes, and more...

 http://www.cplusplus.com/doc/tutorial/polymorphism/
Last edited on
`For example what means L l;? and etc`

This line simply declares a object called `l` of the class `L`, this is basic class stuff I suggest you read up on classes before even looking at polymorphism. Once you understand classes and class members then you can start looking into polymorphism.

 ``1234`` ``````void test2() { R* r = new R(); delete r; } ``````

This is dynamic allocation of an object of `class R` the `delete r;` is returning the memory that the object occupied. If you don't understand this you need to look into dynamic memory allocation as well.

There are a great many sources of tutorials and books out there, that cover these topics. I read the book "Jumping into c++" i found the explanations of the author easy to understand you can obtain the book here for a fee or there a free online tutorials available as well. http://www.cprogramming.com/
Last edited on
Topic archived. No new replies allowed.