Top Banner
Virtual Virtual Functions Functions Chapter: Chapter: 11 11 Lecture: 45 (Date: Lecture: 45 (Date: 06.11.2012) 06.11.2012) Lecture: 46 (Data: Lecture: 46 (Data: 07.11.2012) 07.11.2012)
20

Lec 45.46- virtual.functions

Nov 19, 2014

Download

Documents

Princess Sam

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lec 45.46- virtual.functions

Virtual FunctionsVirtual Functions

Chapter: Chapter: 1111

Lecture: 45 (Date: 06.11.2012)Lecture: 45 (Date: 06.11.2012)

Lecture: 46 (Data: 07.11.2012)Lecture: 46 (Data: 07.11.2012)

Page 2: Lec 45.46- virtual.functions

Advanced C++ TopicsAdvanced C++ Topics

Virtual functionsVirtual functions Friend functionsFriend functions Static functionStatic function Overloaded “= ” operatorOverloaded “= ” operator Overloaded Copy ConstructorOverloaded Copy Constructor ““this” pointerthis” pointer

Page 3: Lec 45.46- virtual.functions

Class Class (Objects, data, functions and (Objects, data, functions and memory)memory)

Page 4: Lec 45.46- virtual.functions

class SmallObjclass SmallObj{{

private:private: int data1, data2;int data1, data2;

public:public: void set_data(int d1, void set_data(int d1, int d2) int d2)

{ data1 = d1; { data1 = d1; data2 = d2; }data2 = d2; }

void show_data() void show_data() {{ cout << cout <<

data1 <<endl data1 <<endl << data2 <<endl;<< data2 <<endl;

}}

};};

int main()int main()

{{ SmallObj obj1, obj2;SmallObj obj1, obj2;

obj1.set_data(1066, obj1.set_data(1066, 5555); 5555);

obj2.set_data(1776, obj2.set_data(1776, 1234);1234);

obj1.show_data(); obj1.show_data();

obj2.show_data();obj2.show_data(); }}

Page 5: Lec 45.46- virtual.functions

Class Class (Objects, data, functions and (Objects, data, functions and memory)memory)

Page 6: Lec 45.46- virtual.functions

Static Class Data and Static Class Data and FunctionsFunctions

Data and member functions in a class can Data and member functions in a class can be made be made staticstatic..

For a statically declared data item, the For a statically declared data item, the compiler creates only one instance of that compiler creates only one instance of that item no matter how many objects of the item no matter how many objects of the same class are created. Precisely, all the same class are created. Precisely, all the objects of the class see the same data; objects of the class see the same data; application e.g., road-racing game; e.g, application e.g., road-racing game; e.g, count.count. e.g.,e.g.,

private:private:

staticstatic int data_item; int data_item; //data_item declared statically//data_item declared statically

Page 7: Lec 45.46- virtual.functions

class fooclass foo{{

private:private: int count;int count;

public:public: foo() : count(0)foo() : count(0)

{ count +{ count ++ ; }+ ; }

int get_count() int get_count() {{ return return

count ; count ; }}};};

int main()int main()

{{

foo f1, f2, f3;foo f1, f2, f3;

cout<< “count is ” << cout<< “count is ” <<

f1.get_count() <<endl; f1.get_count() <<endl;

cout<< “count is ” << cout<< “count is ” <<

f2.get_count() <<endl;f2.get_count() <<endl;

cout<< “count is ” << cout<< “count is ” <<

f3.get_count() <<endl;f3.get_count() <<endl;

}}

Automatic Class DataAutomatic Class Data

Page 8: Lec 45.46- virtual.functions

class fooclass foo{{

private:private: staticstatic int count; int count;

public:public: foo() foo()

{ count +{ count ++ ; }+ ; }

int get_count() int get_count() {{ return return

count ; count ; }}};};

int foo : : count = 0;int foo : : count = 0;

int main()int main()

{{

foo f1, f2, f3;foo f1, f2, f3;

cout<< “count is ” << cout<< “count is ” <<

f1.get_count() <<endl; f1.get_count() <<endl;

cout<< “count is ” << cout<< “count is ” <<

f2.get_count() <<endl;f2.get_count() <<endl;

cout<< “count is ” << cout<< “count is ” <<

f3.get_count() <<endl;f3.get_count() <<endl;

}}

Static Class DataStatic Class Data

Page 9: Lec 45.46- virtual.functions

Static Class DataStatic Class Data

Page 10: Lec 45.46- virtual.functions

Static FunctionsStatic Functions

Like data in a class, member functions Like data in a class, member functions can also be made static.can also be made static.

A static member function follows the A static member function follows the keyword static in its declarator.keyword static in its declarator. e.g.,e.g.,

void mem_func()void mem_func() //normal function declarator//normal function declarator

{{ } }

staticstatic void mem_func() void mem_func() //static function //static function declaratordeclarator

{{ } }

Page 11: Lec 45.46- virtual.functions

class gammaclass gamma{ private:{ private: staticstatic int total; int total;public:public: gamma() gamma() //constructor//constructor

{ total++; }{ total++; }

staticstatic show_total() show_total() {{ cout << “Total cout << “Total

is ” << is ” << << << total <<endl;total <<endl;

}}

};};

int gamma : : total = int gamma : : total = 0; 0; //definition //definition

int main()int main()

{{

gamma g1;gamma g1;

gamma :: gamma :: show_total();show_total();

gamma g2, g3;gamma g2, g3;

gamma :: gamma :: show_total();show_total();

}}

Page 12: Lec 45.46- virtual.functions

class gammaclass gamma{ private:{ private: staticstatic int total; int total; int id;int id;public:public: gamma() gamma() //constructor//constructor

{ total++; { total++; id = total; }id = total; }

staticstatic show_total() show_total() {{ cout << “Total is cout << “Total is

” << ” << << total << total <<endl;<<endl;

}}

void show_id()void show_id(){{ cout << “Id nr. is cout << “Id nr. is

” << ” << << id << id <<endl;<<endl;

}}

};};

int gamma : : total = 0; int gamma : : total = 0; //definition //definition

int main()int main()

{{

gamma g1;gamma g1;

gamma :: gamma :: show_total();show_total();

gamma g2, g3;gamma g2, g3;

gamma :: gamma :: show_total();show_total();

g1.show_id(); g1.show_id();

g2.show_id();g2.show_id();

g3.show_id();g3.show_id(); }}

Page 13: Lec 45.46- virtual.functions

Advanced C++ TopicsAdvanced C++ Topics

Virtual functionsVirtual functions Friend functionsFriend functions Static functionStatic function Overloaded “= ” operatorOverloaded “= ” operator ““this” pointerthis” pointer Overloaded Copy ConstructorOverloaded Copy Constructor

Page 14: Lec 45.46- virtual.functions

Assignment OperatorAssignment Operator

If If a1a1 and and a2a2 are objects then the are objects then the statement statement a1 = a2 a1 = a2 will cause the will cause the compiler to copy data from a1, member compiler to copy data from a1, member by member, into a2. by member, into a2. This is the default This is the default action of the assignment operator.action of the assignment operator.

Page 15: Lec 45.46- virtual.functions

class alphaclass alpha{ {

private:private: int data;int data;

public: public: alpha() {alpha() { } } alpha(int d) { data = d; }alpha(int d) { data = d; }

void display()void display() { cout<<data; }{ cout<<data; }

apha operator = (alpha& a) apha operator = (alpha& a) { data = a.data; { data = a.data; cout << cout << "\nAssignment "\nAssignment operator invoked";operator invoked";

return alpha(data);return alpha(data); }}};};

int main()int main()

{{

alpha a1(5);alpha a1(5);

alpha a2;alpha a2;

a2 = a1; a2 = a1; //invoke //invoke overloaded =overloaded =

cout<<"\n a2= "; cout<<"\n a2= ";

a2.display(); a2.display();

}}

Page 16: Lec 45.46- virtual.functions

class alphaclass alpha{ {

private:private: int data;int data;

public: public: alpha() {alpha() { } } alpha(int d) { data = d; }alpha(int d) { data = d; }

void display()void display() { cout<<data; }{ cout<<data; }

alphaalpha&& operator = (alpha& a) operator = (alpha& a) { data = a.data; { data = a.data; cout << cout << "\nAssignment "\nAssignment operator invoked";operator invoked";

return return *this*this;; }}};};

int main()int main()

{{

alpha a1(5);alpha a1(5);

alpha a2;alpha a2;

a2 = a1; a2 = a1; //invoke //invoke overloaded =overloaded =

cout<<"\n a2= "; cout<<"\n a2= ";

a2.display(); a2.display();

}}

Page 17: Lec 45.46- virtual.functions

This PointerThis Pointer

The member functions of every object have The member functions of every object have access to a sort of magic pointer named access to a sort of magic pointer named thisthis, which points to the object itself., which points to the object itself.

Using Using thisthis pointer any member function pointer any member function can find out the address of the object of can find out the address of the object of which it is a member.which it is a member.

Page 18: Lec 45.46- virtual.functions

class whereclass where

{{

private:private:

char charray[10]; char charray[10]; //occupies 10 //occupies 10 bytesbytes

public:public:

void reveal()void reveal()

{ {

cout << “\nObject’s address is “ cout << “\nObject’s address is “ << this; }<< this; }

};};

int main()int main()

{{

wherewhere w1, w2, w3; w1, w2, w3;

w1.reveal(); w1.reveal();

w2.reveal();w2.reveal();

w3.reveal();w3.reveal();

}}

Page 19: Lec 45.46- virtual.functions

Copy Copy Initialization/ConstructorInitialization/Constructor

For the statement For the statement alpha a2(a1); alpha a2(a1); the the compiler creates a new object, a2, of compiler creates a new object, a2, of class alpha, and copies the data from class alpha, and copies the data from a1, member by member, into a2. a1, member by member, into a2. The The is the default action of the copy is the default action of the copy constructor.constructor.

Page 20: Lec 45.46- virtual.functions

class alphaclass alpha{ private:{ private: int data;int data;public:public: alpha() {alpha() { } } alpha(int d) { data = d; }alpha(int d) { data = d; }

void display()void display() { cout<<data; }{ cout<<data; }

alpha(alpha& a) alpha(alpha& a) //copy //copy constructorconstructor

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

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

}}};};

int main()int main()

{{

alpha a1(5);alpha a1(5);

alpha a2;alpha a2;

a2 = a1; a2 = a1; //invoke //invoke overloaded =overloaded =

cout<<"\n a2= "; cout<<"\n a2= ";

a2.display(); a2.display();

alpha a3(a1);alpha a3(a1); //invoke copy //invoke copy constructorconstructor

// alpha a3 = a1;// alpha a3 = a1;//equivalent //equivalent definition of a3definition of a3

cout << “\na3=”; cout << “\na3=”;

a3.display();a3.display();

}}