c++_2
c++_2
OBJECT PROGRAMMING
-PART II-
HISTORY
var foo = {name: "foo", one: 1, two: 2}; // object notation {}.
var bar = {two: "two", three: 3}; // Another object.
bar.one // Resolves to 1.
private:
// hidden data from outside world
int total; };
COMPOSITION
void printDate(){
cout<<month <<"/" <<day <<"/" <<year <<endl;
}
private:
int month; int day; int year;
};
INHERITANCE
// Derived class
class Rectangle: public Shape {
public: int getArea() { return (width * height); }
};
ACCESS CONTROL AND INHERITANCE
When deriving a class from a base class, the base class may
be inherited through public, protected or private inheritance
üPublic Inheritance: When deriving a class from a public base
class, public members of the base class become public members
of the derived class and protected members of the base class
become protected members of the derived class. A base
class's private members are never accessible directly from a
derived class, but can be accessed through calls to
the public and protected members of the base class
üProtected Inheritance: When deriving from a protected base
class, public and protected members of the base class
become protected members of the derived class
üPrivate Inheritance: When deriving from a private base
class, public and protected members of the base class
become private members of the derived class
EXAMPLE
// Derived class
class Rectangle: public Shape, public
PaintCost {
#include <iostream>
using namespace std; public: int getArea() { return (width * height); }
};
// Base class Shape
class Shape { int main(void) {
public: Rectangle Rect;
void setWidth(int w) { width = w; } int area; Rect.setWidth(5);
void setHeight(int h) { height = h; } Rect.setHeight(7);
protected: int width; int height; area = Rect.getArea();
};
// Print the area of the object.
// Base class PaintCost cout << "Total area: " << Rect.getArea() <<
class PaintCost { endl;
public:
int getCost(int area) { return area * 70; } // Print the total cost of painting cout <<
}; "Total paint cost: $" << Rect.getCost(area)
<< endl;
return 0;
}
ONE MORE EXAMPLE
class A {
public: int x;
protected: int y;
private: int z; };
class B : public A {
// x is public
// y is protected
// z is not accessible from B };
class C : protected A {
// x is protected
// y is protected
// z is not accessible from C };
xclass Line {
public:
int getLength( void );
Line( int len ); // simple constructor
Line( const Line &obj); // copy constructor
~Line();
private:
int *ptr;
};
class printData {
public:
void print(int i) { cout << "Printing int: " << i
<< endl; }
void print(double f) { cout << "Printing float:
" << f << endl; }
void print(char* c) { cout << "Printing
character: " << c << endl; }
};
// The addition without having overloaded operator + could look like this:
Complex c = a.Add(b);
class Complex {
public:
Complex(double re,double im) :real(re),imag(im) {};
Complex operator+(const Complex& other);
Complex operator=(const Complex& other);
private:
double real; double imag;
};
public: Rectangle() {}
Rectangle (int x, int y) : width(x), height(y) {}
int area() {return width * height;}
friend Rectangle duplicate (const Rectangle&);
};
int main () {
Rectangle foo;
Rectangle bar (2,3);
foo = duplicate (bar);
cout << foo.area() << '\n';
return 0;
}
MULTIPLE INHERITANCE
Animal
Horse Eagle
C* pc = new C;
pc->f();
pc->A::f(); //this calls f() from class A
pc->B::f(); //this calls f() from class B
PITFALLS
A* pa = pc; pc->f();
class Base {
protected:
int iData;
public: Base
Base() { iData = 10;}
};
If the two classes derived from the same base class, and
that base class has one or more members, then those
members will be duplicated in the joining class.
class Base {
public:
Base() {}
void foo() {cout << "1";}
};
class Animal {
public:
void /*non-virtual*/ move(void) {
std::cout << "This animal moves in some way" << std::endl; }