C++ Programmierpraktikum – 03

C++ Programmierpraktikum – 03
C++
Homepage:
http://www.hki.uni-koeln.de/display_course/364
Source of the image used throughout the slides:
http://www.clipartpanda.com/clipart_images/site-kostenlose-cliparts-1502746
This work is licensed under a
Creative Commons Attribution 4.0 International License
Topics for today:
=> overloading constructors
=> overloading functions / methods
=> inheritance between classes
=> multiple inheritance
“overloading constructors” in C++:
one class may have more than one constructor with the
same name but different types or number of parameters!
...
class Number {
public:
Number() { // first constructor without parameter!
cout << "I am instanced without parameter!" << endl;
}
Number(int a) { // second constructor with one parameter!
cout << "I am instanced with one parameter!" << endl;
}
// third constructor with two parameters!
Number(int a, int b) {
cout << "I am instanced with two parameters!" << endl;
}
};
...
The execution is the one that matches the
parameters passed on the object declaration!
Example 1: overloading constructors
...
class Number {
public:
Number() { // first constructor without parameter!
cout << "I am instanced without parameter!" << endl;
}
Number(int a) { // second constructor with one parameter!
cout << "I am instanced with one parameter!" << endl;
}
// third constructor with two parameters!
Number(int a, int b) {
cout << "I am instanced with two parameters!" << endl;
}
};
int main()
{
Number object1(1,2);
Number object2;
Number object3(5);
}
...
system("PAUSE");
return 0;
“inheritance between classes” :
In object-oriented programming, inheritance is a way to
form new classes (instances of which are called objects)
using classes that have already been defined.
Polygon
Rectangle
Square
Triangle
The class Polygon would contain members that are common for three types of
polygon. In our case: width and height (for Square we only need one variable).
Rectangle, Square and Triangle would be its derived classes, with specific
features that are different from one type of polygon to the other.
“inheritance between classes” ... (cont'd)
VoiceOfAnimal
Hogh... hogh!
Miauww!
Cook... cook!
The class VoiceOfAnimal would contain one common member for all
types of animal voices. In our case: voice. Dog, Cat and Rooster
would be its derived classes.
Example 1: overloading functions
...
void function() // first function without parameter!
{
cout << "I am called without parameter!" << endl;
}
void function(int a) // second function with one Integer parameter!
{
cout << "I am called with one Integer parameter!" << endl;
}
void function(double a) // third function with one Double parameter!
{
cout << "I am called with one Double parameter!" << endl;
}
int main()
{
function(2.1);
function();
function(5);
}
...
system("PAUSE");
return 0;
Example 2: overloading methods
...
class MyClass {
public:
void method() { // first method without parameter!
cout << "I am invoked without parameter!" << endl;
}
void method(int a) { // second method with one Integer parameter!
cout << "I am invoked with one Integer parameter!" << endl;
}
// third method with one Double parameter!
void method(double a) {
cout << "I am invoked with one Double parameter!" << endl;
}
};
int main()
{
MyClass object;
object.method(1.5);
object.method();
object.method(53);
}
...
system("PAUSE");
return 0;
Example 3: overloading methods in inheritance between classes
...
class MyClass {
public:
void method(int a) // method with one Integer parameter!
{
cout << "I am invoked with one Integer parameter!" << endl;
}
};
class MySubClass : public MyClass {
void method(double a) // method with one Double parameter!
{
cout << "I am invoked with one Double parameter!" << endl;
}
int main()
{
MySubClass object;
object.method(15);
object.method(5.3);
}
...
system("PAUSE");
return 0;
The method inside MyClass has been
altered by the method inside MySubClass!
Example 4: multiple inheritance
...
class Nachname {
protected:
string nachname;
};
class Vorname {
protected:
string vorname;
};
// class Name derived from two superclasses Nachname and Vorname.
class Name : public Nachname, public Vorname {
public:
Name(string a, string b) {
nachname = a;
vorname = b;
}
void print_name() {
cout << nachname << ", " << vorname << endl;
}
};
int main()
{
Name my_name("Musterfrau","Almut");
my_name.print_name();
}
...
system("PAUSE");
return 0;