+ All Categories
Home > Documents > 1 CSC241: Object Oriented Programming Lecture No 21.

1 CSC241: Object Oriented Programming Lecture No 21.

Date post: 14-Dec-2015
Category:
Upload: katy-goyen
View: 217 times
Download: 1 times
Share this document with a friend
Popular Tags:
23
1 CSC241: Object Oriented Programming Lecture No 21
Transcript
Page 1: 1 CSC241: Object Oriented Programming Lecture No 21.

1

CSC241: Object Oriented Programming

Lecture No 21

Page 2: 1 CSC241: Object Oriented Programming Lecture No 21.

2

Previous Lecture

• Polymorphism Example program– person class

• Virtual destructor– base class destructor is virtual, derived class

destructor also become virtual• Friend functions– can access private member of a class

Page 3: 1 CSC241: Object Oriented Programming Lecture No 21.

3

Today’s Lecture

• Friend function – example program: Distance class– Friend function for functional notation

• Friend classes• static functions

Page 4: 1 CSC241: Object Oriented Programming Lecture No 21.

4

Distance Exampleclass Distance {

private:int feet; float inches;

public: Distance() : feet(0), inches(0.0) { } Distance (float fltfeet) { feet = fltfeet; inches = 12*(fltfeet-feet); }Distance(int ft, float in) : feet(ft), inches(in) { }void showdist() const { cout << feet << “ : ” << inches ; }

Distance operator + ( Distance ) const; };

Distance Distance::operator + ( Distance d2 ) const { int f = feet + d2.feet; float i = inches + d2.inches; if(i >= 12.0) {

i -= 12.0; f++;

} return Distance(f,i); }

When such constructor exists, following statements are allowedd3 = 15.5;d3 = d1 + 10.0;

Page 5: 1 CSC241: Object Oriented Programming Lecture No 21.

5

Cont.

• d3 = 10.5;– 10.5 is passed to one argument constructor, – 10.5 is converted into Distance object– d3 = nameless object of Distance;

• d3 = d1 + 9.75;– 9.75 is converted into Distance object by calling

one argument constructor– Then operator + function is invoked by d1 object

• d3 = 6.5 + d1; • d3 = Distance(6.5) + d1;

Page 6: 1 CSC241: Object Oriented Programming Lecture No 21.

6

Note• When a float value pass as argument to function– Definition: void function (Distance d1) { … }– Call : function (10.75);– One argument constructor is called Distance(float)– Convert float value i.e. 10.75 into Distance object– Call : function (Distance (10.75))– function (Nameless Distance object)

• d3 = 10.75 + d1;– Neither float value 10.75 invoke operator +

function– Nor operator knows to convert float to Distance

Page 7: 1 CSC241: Object Oriented Programming Lecture No 21.

7

Distance Example – Friend functionclass Distance {

.

.

.friend Distance operator + ( Distance, Distance ) const;

};Distance Distance::operator + (Distance d1, Distance d2) const {

int f = d1.feet + d2.feet; float i = d1.inches + d2.inches; if(i >= 12.0)

i -= 12.0; f return Distance(f,i);

}

d3 = 10.0 + d1;

friend function Overloaded () operator took two argument

Page 8: 1 CSC241: Object Oriented Programming Lecture No 21.

8

friends for Functional Notationclass Distance {

private:int feet;float inches;

public: Distance() : feet(0), inches(0.0) { }Distance(int ft, float in) : feet(ft), inches(in) { }void showdist() const{ cout << feet << “ : ” << inches ; }float square();

};

float Distance::square() { float fltfeet = feet + inches/12; float feetsqrd = fltfeet * fltfeet; return feetsqrd;

}

main() {Distance dist(3, 6.0); float sqft;sqft = dist.square(); cout << “\nDistance = “; dist.showdist();cout << “\nSquare = “ << sqft;

}

sqft = square(dist);

Page 9: 1 CSC241: Object Oriented Programming Lecture No 21.

9

Cont.class Distance { private:

int feet;float inches;

public: Distance() : feet(0), inches(0.0){ } Distance(int ft, float in) : feet(ft), inches(in){ }void showdist() const{ cout << feet << “:” << inches; }friend float square(Distance);

};

float square(Distance d) {

float fltfeet; fltfeet = d.feet + d.inches/12; float feetsqrd = fltfeet * fltfeet; return feetsqrd;

}

In general, the friend version of a function requires one more argument than when the function is a member.

Go to program

Page 10: 1 CSC241: Object Oriented Programming Lecture No 21.

10

Friend classes

• Same as a function can be friend of a class– Friend function can access private data member

• A class can be friend of another class – All functions of friend class become friend

function of that class

Page 11: 1 CSC241: Object Oriented Programming Lecture No 21.

11

Cont.class alpha {

private: int data1;public:alpha() : data1(99) { } friend class beta;

};

main() {alpha a;beta b;b.func1(a);b.func2(a);cout << endl;

}

class beta { public: void func1(alpha a) { cout << “\ndata1=” << a.data1; }void func2(alpha a) { cout << “\ndata1=” << a.data1; }

};

• In class alpha the entire class beta is proclaimed a friend. • Now all the member functions of beta

can access the private data of alpha

Page 12: 1 CSC241: Object Oriented Programming Lecture No 21.

12

Static function

• A static data member is not duplicated for each object; rather a single data item is shared by all objects of a class

• Example: a class that keep track of how many objects of itself have created

• Function can also be static• A static function can only access static data

member of class

Page 13: 1 CSC241: Object Oriented Programming Lecture No 21.

13

Example programclass gamma {

private:static int total;int id;

public:gamma() { total++; id = total;

}~gamma() {total--;cout << “Destroying ID “ << id << endl;

}

static void showtotal() {cout << “Total is “ << total;

}void showid() {

cout << “ID number is “ << id;}

};int gamma::total = 0;

Page 14: 1 CSC241: Object Oriented Programming Lecture No 21.

14

Static vs. non static functions

• Static function can only access static data member of class– className::static_function();– Object_of_class.static function();

• Non static member function can access static and non static data member of class– Object_of_class.Non_static_function();

Go to program

Page 15: 1 CSC241: Object Oriented Programming Lecture No 21.

15

Assignment and Copy Initialization

• C++ compiler do many things on programmer behalf. For example:– Assignment operator– Copy constructor

Assignmenta2 = a1;

Copy constructoralpha a2(a1);

cause the compiler to copy the data from a1, member by member, into a2.

compiler creates an object, a2, copies the data from a1, member by member, into a2

Page 16: 1 CSC241: Object Oriented Programming Lecture No 21.

16

Cont.

• Both of these default activities are provided, free of charge, by the compiler

• If member-by member copying is required, then default activities are enough

• If assignment or initialization has to do something more complex, then they must be override

• First, separate example to override default assignment and copy constructor

• String class in more efficient way by overriding these two activities

Page 17: 1 CSC241: Object Oriented Programming Lecture No 21.

17

Overloading the Assignment Operatorclass alpha {

private: int data;public:

alpha() { }alpha(int d) { data = d; }void display(){ cout << data; }

alpha operator = (alpha& a){data = a.data; cout << “Assignment operator invoked”;return alpha(data);

}};

main() {alpha a1(37);alpha a2;a2 = a1; cout << “\na2=”; a2.display(); alpha a3 = a2; cout << “\na3=”; a3.display();

}

Program OutputAssignment operator invokeda2=37a3=37

Go to program

Page 18: 1 CSC241: Object Oriented Programming Lecture No 21.

18

Note

• Initialization Is Not Assignment– alpha a3 = a2; // copy initialization

is not an assignment but an initialization, with the same effect as– alpha a3(a2);

• Passing by Reference– Save memory space– Avoid creation of unwanted objects

• Returning a Value- Cannot do return by reference

Page 19: 1 CSC241: Object Oriented Programming Lecture No 21.

19

Assignment operator – inheritance

• The assignment operator is unique among operators in that it is not inherited

• If you overload the assignment operator in a base class, you can’t use this same function in any derived classes

Page 20: 1 CSC241: Object Oriented Programming Lecture No 21.

20

The Copy Constructor

• An object can be define and initialize with values of another object of same class– alpha a3(a2); // copy initialization– alpha a3 = a2; // copy initialization, alternate syntax

• Both invoke a copy constructor: – That creates a new object and copies its argument into

it• Default copy construct perform member by

member copy• Distance d2 = d1; 7

6.5feet

inches

d1feet

inches

d27

6.5

Page 21: 1 CSC241: Object Oriented Programming Lecture No 21.

21

Cont.

• It is similar to what assignment operator (d2 = d1) does

• The difference is that copy initialization also creates a new object

Page 22: 1 CSC241: Object Oriented Programming Lecture No 21.

22

Example program – assignment and copy constructor

class alpha {private: int data;public:

alpha() { }alpha(int d){ data = d; }alpha(alpha& a) {

data = a.data;cout << “\nCopy constructor invoked”;

}

void display(){ cout << data; }void operator = (alpha& a) {data = a.data;cout << “\nAssignment operator invoked”;

}};

a2 = a1; alpha a3(a1);

Program OutputAssignment operator invokedCopy constructor invoked

Page 23: 1 CSC241: Object Oriented Programming Lecture No 21.

23


Recommended