Object inheritance

Hi guys so I made a very simple program and I was quite surprised by the results I'm wondering why twoObject can call the function printIt when printIt does not belong to ObjectTwo??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include <iostream>

using namespace std;

class one{

  public:
     int age;

    one(int x){

        age = x;
    }

    void printIt(){

        cout << age;
    }

  private:
};

class two{

   public:
       two(one oo);

   private:
};

int main()
{
    one oneObject(11);
    one twoObject(oneObject);
    twoObject.printIt();

}
Line 34: twoObject is an instance of class one. printIt is a public member of class one, so it's perfectly legal to call.

Bad naming practice here. Why are you calling an object oif type one a "twoObject"?.
Topic archived. No new replies allowed.