DPOB LI0 Object programming - classes 3
This document contain information about labolatory exercises.
Object programming - classes 3
Subject - Polimorphism, abstract classes, copy constructor, dynamic object creation, initialize data in costructors.
1. Polimorfizm
1. Basing on classes Person, Student and Worker create a program which counts incomes of people from university.
a) Create virtual method f loat income() in class Person and define table which would store Persons.
b) Create global function spending() that will count total income of pe- ople connected to university as sum of incomes of each person.
c) Change table of pointers to the table of objects. Check whatever the polymorphism mechanism works correctly ? Does it work if you delete a word virtual from income method ?
d) Change method income in class Person to pure virtual method. Are you now allowed to create objects of type Person? What will change if you comment definition of method income in class Student?
2. Constructors, copy Constructors
1. Open file heap.cpp
a) Watch how to dynamic create and delete objects in C++.
b) Add to class Heap constructor with two arguments (string, string) that will set field type and name from parent class Container.
c) Using new create in main() new object Heap with a given name ’Dy- namic created Heap’. Invoke method W riteContainerM ethod() on him.
d) Add copy constructor to class Container(const&Container). Check what now happened when you invoke method W riteContainerM ethod()
?. What is the difference when copy constructor is set and when it is commented?
e) Execute a method W riteHeapElements() two times one by another.
f) Define in Heap new arg-constructor with argument type const char∗.
Execute method W riteHeapElements() with string parameter string a what will happened ? How to prevent such beheviour?
3. Do it yourself tasks
1. Create properly working copy constructor for class Heap (pointers should point to new place in memory).
1
DPOB LI0 Object programming - classes 3
2. Change Heap class so that it could operate on different data types (string, float, double).
2