0
Chapter: 11    Virtual Functions       Lecture: 45 (Date: 06.11.2012)       Lecture: 46 (Data: 07.11.2012)
Advanced C++ Topics   Virtual functions   Friend functions   Static function   Overloaded “= ” operator   Overloaded ...
Class (Objects, data, functions and memory)
class SmallObj                   int main(){                                  {                                       Smal...
Class (Objects, data, functions and memory)
Static Class Data and Functions   Data and member functions in a class can be made    static.   For a statically declare...
Automatic Class Dataclass foo                    int main(){                            {private:                        f...
Static Class Dataclass foo                    int main(){                            {private:                        foo ...
Static Class Data
Static Functions   Like data in a class, member functions can also be    made static.   A static member function follows...
class gamma                             int main(){ private:                                {    static int total;public: ...
class gamma                             int main(){ private:                                {    static int total;    int ...
Advanced C++ Topics   Virtual functions   Friend functions   Static function   Overloaded “= ” operator   “this” poin...
Assignment Operator   If a1 and a2 are objects then the statement a1 = a2    will cause the compiler to copy data from a1...
class alpha                                     int main(){                                                {private:      ...
class alpha                                     int main(){                                                {private:      ...
This Pointer   The member functions of every object have access to a    sort of magic pointer named this, which points to...
class where                             int main(){                                       {private:                       ...
Copy Initialization/Constructor   For the statement alpha a2(a1); the compiler    creates a new object, a2, of class alph...
class alpha                                     int main(){ private:    int data;                                   {publi...
Upcoming SlideShare
Loading in...5
×

Lec 45.46- virtual.functions

686

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
686
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Student Book
  • Transcript of "Lec 45.46- virtual.functions"

    1. 1. Chapter: 11 Virtual Functions Lecture: 45 (Date: 06.11.2012) Lecture: 46 (Data: 07.11.2012)
    2. 2. Advanced C++ Topics Virtual functions Friend functions Static function Overloaded “= ” operator Overloaded Copy Constructor “this” pointer
    3. 3. Class (Objects, data, functions and memory)
    4. 4. class SmallObj int main(){ { SmallObj obj1, obj2;private: int data1, data2; obj1.set_data(1066, 5555);public: void set_data(int d1, int d2) { data1 = d1; obj2.set_data(1776, 1234); data2 = d2; }void show_data() obj1.show_data(); { cout << data1 <<endl obj2.show_data(); << data2 <<endl; } }};
    5. 5. Class (Objects, data, functions and memory)
    6. 6. Static Class Data and Functions Data and member functions in a class can be made static. For a statically declared data item, the compiler creates only one instance of that item no matter how many objects of the same class are created. Precisely, all the objects of the class see the same data; application e.g., road-racing game; e.g, count.  e.g., private: static int data_item; //data_item declared statically
    7. 7. Automatic Class Dataclass foo int main(){ {private: foo f1, f2, f3; int count; cout<< “count is ” <<public: f1.get_count() <<endl; foo() : count(0) { count ++ ; } cout<< “count is ” <<int get_count() f2.get_count() <<endl; { return count ; }}; cout<< “count is ” << f3.get_count() <<endl; }
    8. 8. Static Class Dataclass foo int main(){ {private: foo f1, f2, f3; static int count; cout<< “count is ” <<public: f1.get_count() <<endl; foo() { count ++ ; } cout<< “count is ” <<int get_count() f2.get_count() <<endl; { return count ; }}; cout<< “count is ” <<int foo : : count = 0; f3.get_count() <<endl; }
    9. 9. Static Class Data
    10. 10. Static Functions Like data in a class, member functions can also be made static. A static member function follows the keyword static in its declarator.  e.g., void mem_func() //normal function declarator { } static void mem_func()//static function declarator { }
    11. 11. class gamma int main(){ private: { static int total;public: gamma g1; gamma() //constructor gamma :: show_total(); { total++; }static show_total() gamma g2, g3; { cout << “Total is ” << gamma :: show_total(); << total <<endl; }}; }int gamma : : total = 0; //definition
    12. 12. class gamma int main(){ private: { static int total; int id; gamma g1;public: gamma :: show_total(); gamma() //constructor { total++; id = total; } gamma g2, g3;static show_total() gamma :: show_total(); { cout << “Total is ” << << total <<endl; } g1.show_id();void show_id() { cout << “Id nr. is ” << g2.show_id(); << id <<endl; g3.show_id(); }}; }int gamma : : total = 0; //definition
    13. 13. Advanced C++ Topics Virtual functions Friend functions Static function Overloaded “= ” operator “this” pointer Overloaded Copy Constructor
    14. 14. Assignment Operator If a1 and a2 are objects then the statement a1 = a2 will cause the compiler to copy data from a1, member by member, into a2. This is the default action of the assignment operator.
    15. 15. class alpha int main(){ {private: alpha a1(5); int data; alpha a2;public: alpha() { } a2 = a1; //invoke overloaded = alpha(int d) { data = d; } cout<<"n a2= "; void display() { cout<<data; } a2.display(); } apha operator = (alpha& a) { data = a.data; cout << "nAssignment operator invoked"; return alpha(data); }};
    16. 16. class alpha int main(){ {private: alpha a1(5); int data; alpha a2;public: alpha() { } a2 = a1; //invoke overloaded = alpha(int d) { data = d; } cout<<"n a2= "; void display() { cout<<data; } a2.display(); } alpha& operator = (alpha& a) { data = a.data; cout << "nAssignment operator invoked"; return *this; }};
    17. 17. This Pointer The member functions of every object have access to a sort of magic pointer named this, which points to the object itself. Using this pointer any member function can find out the address of the object of which it is a member.
    18. 18. class where int main(){ {private: where w1, w2, w3;char charray[10]; //occupies 10 bytespublic: w1.reveal();void reveal() w2.reveal();{ w3.reveal(); cout << “nObject’s address is “ <<this; } }};
    19. 19. Copy Initialization/Constructor For the statement alpha a2(a1); the compiler creates a new object, a2, of class alpha, and copies the data from a1, member by member, into a2. The is the default action of the copy constructor.
    20. 20. class alpha int main(){ private: int data; {public: alpha a1(5); alpha() { } alpha a2; alpha(int d) { data = d; } void display() a2 = a1; //invoke overloaded = { cout<<data; } alpha(alpha& a) //copy constructor cout<<"n a2= "; { data = a.data; a2.display(); cout << “nCopy constructor invoked”; } alpha a3(a1); //invoke copy constructor void operator = (alpha& a) // alpha a3 = a1;//equivalent definition of a3 { data = a.data; cout << "nAssignment operator invoked"; cout << “na3=”; } a3.display();}; }
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×