23. class Shape { protected : int width, height; public : void setDims ( int a, int b){ width=a; height=b;} }; class Rectangle: public Shape { public : int area ( ) { return (width * height); } }; class Triangle: public Shape { public : int area ( ) { return (width * height/2); } }; class Square: public Rectangle { public : void setDims ( int a){ width=a; height=a;} };
110. public inheritance protected inheritance private inheritance public member in base class public in derived class protected in derived class private in derived class protected member in base class protected in derived class protected in derived class private in derived class private member in base class hidden in derived class hidden in derived class hidden in derived class
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161. class HourlyEmployee: Employee { private: int hours; public: void getdata(){ { Employee::getdata(); cout<<" Enter number of hours worked...."; cin>>hours; } void toString() { Employee::toString(); cout<<" Hours worked..."<<hours; } double pay() { return payRate * hours; } }; Pay() function must to be redefined
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182. class Wheel { private: int diameter; public: Wheel(int diameter_in){ diameter = diameter_in; } int getDiameter() { return diameter; } }; class Engine { private: int cc; public: Engine(int cc_in) { cc = cc_in; } int getCC()return cc; } };
183. class Car { private: Wheel nearside_front, offside_front, nearside_rear, offside_rear; Engine engine; int passengers; public: Car(int diameter_in, int cc_in, int passengers_in); void showSelf(); };
184. Car::Car(int diameter_in, int cc_in, int passengers_in) : nearside_front(diameter_in), offside_front(diameter_in), nearside_rear(diameter_in), offside_rear(diameter_in), engine(cc_in) { passengers = passengers_in; }
186. HL 1. Main component / sub component Composition Aggregation "contains " relation "has" relation: Main component "has" a sub component as a part. 2. Association "owns" "knows" "uses" "serves“ “ observes” etc. 3. Generalisation / specialisation Inheritance General case / Special case Super class / Sub class Base class / Derived class Base class / Inherited class "is" relation. An instance of derived class "is" an instance of base class.
187. HL Composition means same as - contains - is part of Example 1. Point can be a sub component of Circle. class Circle { public: Circle(float x0=0.0, float y0=0.0, r0=0.0); Circle(const Point &cp0, r0=0.0); private: float r; Point cp; //sub component }; Constuctor implementation on the next page. Example 2. Date can be used as a data member in class Person: class Person { public: Person(const string& name, int year, int month, int day); Person(const string& name, const Date& bd); private: string name; // sub component 1 Date birthDay; // sub component 2 };
188. HL An object can have an object as a subcomponent. In this example circles have a point as a subcomponent (representing their center point */ #include <iostream.h> //Class definition of Point class Point { public: Point(float x0=0.0, float y0=0.0); void read(); void print(); private: float x; float y; }; //Class definition of Circle class Circle{ public: Circle(float x0=0.0, float y0=0.0, float r0=0.0); Circle(const Point &cp0, float r0=0.0); void read(); void print(); private: float radius; Point centerPoint; };
189. HL //Application void main (void) { Point p(11.0, 12.0); Circle c1, c2(1.0, 2.0, 10.0), c3(p, 20.0); c1.print(); c2.print(); c3.print(); } //Member function implementations //Member functions of Point Point::Point(float x0, float y0) { x = x0; y = y0; } void Point :: read() { cout << " x-coordinate: "; cin >> x; cout << " y-coordinate: "; cin >> y; } void Point :: print() { cout << "(" << x << "," << y << ")"; }
190. HL //Member functions of Circle //Constructor 1 version 1 Circle::Circle(float x0, float y0, float r0): centerPoint(x0, y0){ radius = r0; } /* Constructor 1 version 2 (Point constructor is called twice) Circle::Circle(float x0, float y0, float r0) { radius = r0; centerPoint = Point(x0, y0); } */ //Constructor 2 version 1 Circle::Circle(const Point &cp0, float r0):centerPoint(cp0){ radius = r0; } /*Constructor 2 version 2 (Point constructor is called twice) Circle::Circle(const Point &cp0, float r0) { radius = r0; centerPoint = Point(cp0); //Default copy constructor of Point } */ void Circle :: read() { cout << "Enter radius : "; cin >> radius; cout << "Enter centerpoint :"; centerPoint.read(); } void Circle :: print() { cout << "The circle data is :"; cout << "Radius is : " << radius; cout << "Center point is :"; centerPoint.print(); }
191. HL Memory allocation is done at the same time (as one block of memory) for the main component and for sub component, because sub component is part of the memory area of the main component. The object is created in the following phases: 1. Memory is allocated for the whole object. 2. The constructor call of sub component class that is defined in the initialisation list of main component is executed. If there is no sub component constructor call in the initialisation list, the default constructor of sub component is called. 3. The function body of main component constructor is called. This rule of order means that it is guaranteed that sub component is already constructed when constructor body on main component is executed. It also guarantees that sub components are always initialised at least with default constructor of sub component class. This is useful as you can understand thinking about the differences between defining a name member in the Person class as char *name or string name!. The implementor of the Person class needs to do many things in the former case but almost nothing in the latter case.
192. HL When an object containing a sub-component object is de-allocated, the things happen in the following order: 1.The destructor of the main component is called first. 2. The destructor of the sub-component is called next. 3. The memory area is de-allocated as a whole. The default assignment operator of the main component: If we don’t overload the assignment operator in the main component class the default assignment is used. If we have overloaded assignment for the sub-component that is used in the default assignment when sub-component part is copied. The default copy constructor of the main component: If we don’t write the copy constructor for the main component class the default copy constructor is used. If we have written copy constructor for the sub-component it is used in the default copy constructor when sub-component part is constructed.
193.
194.
195.
Notas do Editor
The reusability is a key benefit of OO programming. By reusing the code the development effort is thus reduced. The time can further be spent in making the software reliable and ROBUST
Example: Design an application, teachers and the principal (director) in a school. First, assume that we have a class to define teachers, then we can use this class to model the principal. Because the principal is a teacher.