Class hierarchy 0
main.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <string>
3 using namespace std;
4 
5 
6 
7 class A
8 {
9 public:
10  A(string const & name)
11  : _ss(name)
12  {
13  }
14 
15  string Get_string() const
16  {
17  return _ss;
18  }
19 
20 //private: // No access from outside this class.
21 protected: // Allows access to members by derived classes.
22  string _ss;
23 
24 };
25 
26 class B : public A // IS-A [derived class]; 'public' allows access to public/protected members/methods of class A
27 {
28 public:
29  B(string const & name, int b) // Has-A [additional property wrt. A]
30  : A(name), _b(b)
31  {
32 // cout << _ss << endl;
33  }
34 
35  //string Get_string() const {return _ss;} // overload, not needed here
36 
37  int fkt_B() const
38  {
39  return _b*_b; // Has-A [additional property wrt. A]
40  }
41 
42 private:
43  int _b; // Has-A [additional property wrt. A]
44 };
45 
46 
47 
48 //------------------------
49 
50 int main()
51 {
52  A v("Freitag");
53  cout << v.Get_string() << endl; // A::Get_string
54 
55  B g("Samstag",7);
56 
57  cout << g.Get_string() << " "; // B::Get_string --> A::Get_string
58  cout << g.fkt_B() << endl;
59 
60  return 0;
61 }
Definition: main.cpp:8
A(string const &name)
Definition: main.cpp:10
string _ss
Definition: main.cpp:22
string Get_string() const
Definition: main.cpp:15
Definition: main.cpp:27
B(string const &name, int b)
Definition: main.cpp:29
int fkt_B() const
Definition: main.cpp:37
int main()
Definition: main.cpp:50