Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Object Oriented Programming

Tirgul 3
Classes composition
Copy Constructors
Const objects
Composition vs. Aggregation


:
.R2-
.
:
2 ) 2(.
)( > <x,y .
><x2,y2
><x1,y1

"
// line1.h
#ifndef _LINE_1_H
#define _LINE_1_H
#include <iostream>
using namespace std;
class Line1 {
private:
int m_x1,m_y1,m_x2,m_y2;
int m_color;
public:
Line1(int c, int x1, int y1, int x2, int y2);
void print();
};
#endif // _LINE_1_H

// line1.cpp
#include "line1.h"
Line1::Line1(int c, int x1, int y1, int x2, int y2) {
m_color=c;
m_x1=x1;
m_y1=y1;
m_x2=x2;
m_y2=y2;
}
void Line1::print() {
cout << " color=" << m_color
<< " <" << m_x1 << "," << m_y1 << ">
<< " <" << m_x2 << "," << m_y2 << ">
<< endl;
}

:
:
.1 )x1
( )> (<x,y
.2 )' (print
.3 ),
(..
.4 ) (int .
) double-
(line

Point :
// point2.h
#ifndef _POINT2_H
#define _POINT2_H
#include <iostream>
using namespace std;
class Point2 {
private:
int m_x, m_y;
public:
Point2(int x, int y);
void print();
};

#endif // _POINT2_H

// point2.cpp

#include "point2.h"
Point2::Point2(int x, int y) {
m_x=x;
m_y=y;
}
void Point2::print() {
cout << " <" << m_x << ","
<< m_y << "> << endl;
}

line2
// line2.h

// line2.cpp

#ifndef _LINE_2_H
#define _LINE_2_H

#include "line2.h"

#include "point2.h"
#include <iostream>
using namespace std;

Line2::Line2(int c, int x1, int y1, int x2, int y2) :


m_p1(x1,y1) , m_p2(x2,y2) {
m_color=c;
}

class Line2 {
private:
int m_color;
Point2 m_p1, m_p2;
public:
Line2(int c, int x1, int y1, int x2, int y2);
void print();
};
#endif // _LINE_2_H

void Line2::print() {
cout << " color=" << m_color;
cout << " p1="; m_p1.print();
cout << " p2=; m_p2.print();
}

:constructor-
:
)Line2::Line2(int c, int x1, int y1, int x2, int y2) : m_p1(x1,y1) , m_p2(x2,y2
{
;m_color=c
}

m_p1 m_p2- .line


m_p1( ." )m_p2
data members ( Line2
,)m_color .
destructor- ,Line2 :
m_p2.m_p1 -

:
:
.1 )>(<x,y

.2 )' (print
.3 ) ,(..
.4 ) (double<-int
) ( line
:
.1 Point
,Line .

Point2 :
// line3.h
#ifndef _LINE_3_H
#define _LINE_3_H
#include <iostream>
using namespace std;
class Line3 {
private:
class Point3 {
private:
int m_x, m_y;
public:
Point3(int x, int y) : m_x(x), m_y(y) {}
void print();
};

private:
int m_color;
Point3 m_p1, m_p2;
public:
Line3(int c, int x1, int y1, int x2, int y2)
: m_p1(x1,y1) , m_p2(x2,y2) {
m_color = c;
}
void print();
};
#endif // _LINE_3_H

// line3.cpp
#include "line3.h
void Line3::Point3::print() {
cout << " <" << m_x << "," << m_y << "> \n;
}
void Line3::print() {
cout << " color=" << m_color;
cout << " p1=; m_p1.print();
cout << " p2=; m_p2.print();
}

:
:
Point3
) Line3
Point3
(Line3

Point Constructor <1,2>


#include "line3.h"
int main() {

Point Constructor <3,4>


Line Constructor,
color=0,
p1= <1,2>,
p2= <3,4>

Line3 l3(0,1,2,3,4);

l3.print();
l3.p1.print();
Point3 p3(1,2);
p3.print();
return 0;
}

// ERROR
// ERROR
// ERROR

color=0 p1= <1,2> p2= <3,4>

Line Destructor,
color=0,
p1= <1,2>,
p2= <3,4>
Point Destructor <3,4>
Point Destructor <1,2>

Copy Constructor

// Employee.h
#include <iostream>
using namespace std;
class Employee {
public:
Employee(int ID,
const char* name);
~Employee();
// Methods
void Print();
private:
// members
int m_ID;
char* m_name;
};

// Employee.cpp
#include "Employee.h

Employee::Employee(int iID, const char *name){ // Ctor


cout << "Constructor called" << endl;
m_ID = iID;
m_name = new char [strlen(name) +1];
strcpy(m_name, name);
Print();
}
void Employee::Print() { // Print
cout << "ID: " << m_ID << ",Name: << m_name
<< " ,Address(pointer):0x" << hex << (int) m_name<< endl;
}
Employee::~Employee() { // Dtor
cout << "Destructor called"<<endl;
Print();
delete [] m_name;
m_name=NULL;
}


int main()
{
Employee emp1(3, "Moshiko");
Employee emp2(5, "Yosifun");
emp1.Print();
emp2.Print();
return 0;
}

Output:
Constructor called
ID: 3,Name:Moshiko ,Address(pointer):0x863140
Constructor called
ID: 5,Name:Yosifun ,Address(pointer):0x863284
ID: 3,Name:Moshiko ,Address(pointer):0x863140
ID: 5,Name:Yosifun ,Address(pointer):0x863284
Destructor called
ID: 5,Name:Yosifun ,Address(pointer):0x863284
Destructor called
ID: 3,Name:Moshiko ,Address(pointer):0x863140

void MyFunc(Employee emp) {

Output:
cout << "in MyFunc \n ";
Constructor called
ID: 1,Name:Eli Rachamim ,Address(pointer):0x863194

in MyFunc
int main()
{
Destructor called
Employee One(1,"Eli Rachamim"); ID: 1,Name:Eli Rachamim ,Address(pointer):0x863194
// calling copy constructor
MyFunc(One);
cout<< "In the end of main \n;
// Here the program crashes!
return 0;
}

In the end of main


Destructor called
ID: 1,Name: ,Address(pointer):0x863194

// Employee.h

class Employee
{
public:
Employee(int ID,
const char *name);
// Copy constructor
Employee (const Employee & );
// Destructor
~Employee();
void Print();
private:
// attributes
int m_ID;
char* m_name;
};

// Employee.cpp

// Copy constructor
Employee::Employee(const Employee & Emp) {
cout << "Copy Constructor called" << endl;
m_ID = Emp.m_ID;
m_name=new char
[strlen(Emp.m_name) +1];
strcpy(m_name,Emp.m_name);
Print();
}


void MyFunc(Employee emp) {

Output:

cout << "in MyFunc \n ";


Constructor called
ID: 1,Name:Eli Rachamim ,Address(pointer):0x863140

Copy Constructor called


int main()
ID: 1,Name:Eli Rachamim ,Address(pointer):0x863288
{
Employee One(1,"Eli Rachamim");
in MyFunc
MyFunc(One);

Destructor called
ID: 1,Name:Eli Rachamim ,Address(pointer):0x863288

cout<< "In the end of main \n;


In the end of main
return 0;

Destructor called
ID: 1,Name:Eli Rachamim ,Address(pointer):0x863140

Copy Constructor

()C.Ctor

:
.1 ) by reference C.Ctor
) (Dtor (
.2 C.Ctor - private- ,
.
, .by value
, !
) C.Ctor- private- byVal
(clone

// Point.h
#ifndef _POINT_
#define _POINT_
#include <iostream>
using namespace std;
class Point
{
private:
int m_iX, m_iY;
public:
Point (int valX, int valY);
~Point (); //destructor
void Show ();
int GetX() { return m_iX; }
int GetY() { return m_iY; }
//Distance between 2 points.
double Distance(Point p1);
};
#endif

// Point.cpp
#include "point.h"
#include <math.h>
// Calculates the distance between the
// two points.
double Point::Distance(Point p1) {
return (sqrt( pow(m_iX-p1.m_iX,2)
+ pow(m_iY-p1.m_iY,2)));
}
void Point::Show (){
cout<<"x="<<m_iX<<"
y="<<m_iY<<endl;
}
Point::Point(int valX, int valY)
: m_iX(valX), m_iY(valY) {}
Point::~Point() {// destructor
cout << "GoodBye:";
Show();
}

// Circle.h

#ifndef _CIRCLE_
#define _CIRCLE_
#include "point.h"
class Circle {
private:
Point m_Center;
int m_iRadius;

public:
Circle(); // Default constructor
// Initilization list constructor
Circle(Point CenterPoint,int iRadius);
~Circle() { // Destructor
cout << "circle destructor"<<endl;
Print();
}
Point MoveCenter (const Point& NewCenter);
bool IsInside
(const Point& Location );
bool IsOverlap (const Circle& NewCircle);
void Print();
};
#endif

// Circle.cpp
#include "circle.h"
#include <math.h>
Circle::Circle() // Default constructor
: m_Center(0,0), m_iRadius(1) {
cout <<"Circle default constructor\n";
Print();
}
Circle::Circle(Point centerPoint, int iRadius)
: m_Center(centerPoint),
m_iRadius(iRadius) {
cout << "Circle init list constructor\n";
Print();
}
Point Circle::MoveCenter(const Point&
newCntr) {
Point lastCenter=m_Center;
m_Center= newCntr;
return lastCenter;
}

// Circle.cpp
bool Circle::IsInside(const Point& Location) {
double dDistance =
m_Center.Distance(Location);
return (dDistance<m_iRadius);
}
bool Circle::IsOverlap(const Circle &
NewCircle) {
double dDistance =
m_Center.Distance(NewCircle.m_Center);
int iRadiusLen =
m_iRadius+NewCircle.m_iRadius;
return ( dDistance < iRadiusLen );
}
void Circle::Print() {
cout << "center:";
m_Center.Show();
cout << "radius:" << m_iRadius << endl;
}

#include "circle.h"

void main() {
Point p1(12,12);
Circle c1;
Circle c2(p1,5);
Point p2(4,3);
cout <<c1.IsInside(p2)<<endl;
cout <<c1.IsOverlap(c2)<<endl;

c2.MoveCenter(p2);
cout <<c1.IsOverlap(c2)<<endl;
}

Output:
(1) Circle default constructor
(2) center:x=0 y=0
(3) radius:1
(4) Circle init list constructor
(5) center:x=12 y=12
(6) radius:5
(7) GoodBye:x=12 y=12
(8) GoodBye:x=4 y=3
(9) 0
(10) GoodBye:x=12 y=12
(11) 0
(12) GoodBye:x=12 y=12
(13) GoodBye:x=12 y=12
(14) GoodBye:x=4 y=3
(15) 1
(16) GoodBye:x=4 y=3
(17) circle destructor
(18) center:x=4 y=3
(19) radius:5
(20) GoodBye:x=4 y=3
(21) circle destructor
(22) center:x=0 y=0
(23) radius:1
(24) GoodBye:x=0 y=0
(25) GoodBye:x=12 y=12

const

:const
const .
:
private:
;const int m_x, m_y
public:
{ )Point(int x, int y
m_x=x; // error
m_y=y; // error
}
;}

:const
const constructor-
: constructor-
class Point {
private:
const int m_x, m_y;
public:
Point(int x, int y) : m_x(x), m_y(y) { }
};

Const Member Variables & Functions


- const
, :
' ) (print const

const
, const instance- ,
.

// File: String.h

class String
{
private:
char* m_pStr;
int
m_iLength;
public:
String(const char*);
char* GetStr();
void Reverse();
// returns the position of the char
// or -1 if not found
int Find(char) const;
};

// File: String.cpp
String::String(const char* str) {
m_iLength = strlen(str);
m_pStr = new char[m_iLength+1];
strcpy(m_pStr,str);


}
int String::Find(char to_find) const
{}
char* String::GetStr() { return m_pStr; }
void String::Reverse() { }
void PrintVarName(String & var_str)
{}
// File: main.cpp
void main() {
String var_name("m_sName");
PrintVarName(var_name);
}

' PrintVarName .:
}{ )void PrintVarName(const String & var_str
. :
{ )void PrintVarName(const String & var_str

;)(var_str.GetStr

}
:
const -

// File: String.h

// File: String.cpp

const char* String::GetStr() const


class String
{
private:
char *m_pStr;
int m_iLength;
public:
String(const char*);

const char* GetStr() const;


char operator[] (int) const;
char& operator[] (int);
void Reverse();
// returns the position of the char
// or -1 if not found
int Find(char) const;
};

{ return m_pStr; }
char String::operator[] (int pos) const
{}
char& String::operator[] (int pos) { }
void PrintVarName(const String& v) const
{

v.GetStr(); // now it is OK
}
// File: main.cpp
void main() {
String var_name("m_sName");
PrintVarName(var_name);
}

Temporary Objects
PrintVarName("m_sName") :
.const String :
PrintVarName '
void PrintVarName(const String & v) {
...
}
const String .

void PrintVarName(String & v) {


...
}

const String :
String&-

void PrintVarName(String v) {
...
.by value .
}
C.Ctor

Const Object Pointer


// Employee.h

// Employee.cpp

class Employee
{
private:
String m_sName;
const String *m_pBossName;

Employee::Employee(const String&
name, const String* pBossName)
: m_sName(name) {
m_pBossName = pBossName;
}

public:
Employee(const String &name,
const String *pBossname);
void Func();
};

void Employee::Func()
{
// ...
m_pBossName->reverse();
}

:
(const
, reverse
const
const- )

Composition vs. Aggregation

Composition vs. Aggregation


Composition:
when one of the
class's variable is an
object

Aggregation:
when one of the class's
variable is a pointer to
an object.

class B {....};
class A {
private:
B b;
//.....
};

class B {....};
class A2 {
private:
B* b;
//.....
};

Composition

:
.1 B ;}class B {....
.A
{ class A
private:
) .2 ( :
;B b
) B (
//.....
.3
;}
) (..delete
.4 : composition -
B
)
,
(

Composition
) .5 (memory :
;}class B {....
defragmentation dynamic
{ class A
allocation .heap-
private:
,
;B b
"" heap-
//.....
) .(disk defragmentation-
;}
) .6 ( composition :
)( , 2
.pointer- -
.page fault

Aggregation

:
.1 B A
;}class B {....
)
{ class A
( . : B
private:
) , , '(,
;B* b
-
//.....
) ,
;}
' , (A
) .2 ( :
exception ) B
constructor
(A

Aggregation

) .3 ( : B
) ,(interface ;}class B {....

{ class A
"
private:
.
interface

;B* b
//.....
;}

You might also like