SlideShare uma empresa Scribd logo
1 de 20
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 Copy Constructor
   “this” pointer
Class (Objects, data, functions and memory)
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;
       }                           }
};
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 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
Automatic Class Data
class 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;
                               }
Static Class Data
class 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;
                               }
Static Class Data
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
           {       }
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
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
Advanced C++ Topics

   Virtual functions
   Friend functions
   Static function
   Overloaded “= ” operator
   “this” pointer
   Overloaded Copy Constructor
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.
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);
    }
};
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;
    }
};
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.
class where                             int main()
{                                       {
private:
                                          where w1, w2, w3;
char charray[10]; //occupies 10 bytes

public:                                     w1.reveal();
void reveal()                               w2.reveal();
{                                           w3.reveal();
  cout << “nObject’s address is “ <<
this; }                                 }
};
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.
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();
};
                                                }

Mais conteúdo relacionado

Mais procurados

Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad Fabernovel
 
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...tdc-globalcode
 
3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в JavaDEVTYPE
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshuSidd Singh
 
Functor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadOliver Daff
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript FunctionsColin DeCarlo
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript FunctionsBrian Moschel
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.Russell Childs
 
The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84Mahmoud Samir Fayed
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: FunctionsAdam Crabtree
 
User defined functions
User defined functionsUser defined functions
User defined functionsshubham_jangid
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overviewgourav kottawar
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Datagreenwop
 

Mais procurados (20)

Talk - Query monad
Talk - Query monad Talk - Query monad
Talk - Query monad
 
P1
P1P1
P1
 
Pointers
PointersPointers
Pointers
 
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java3. Объекты, классы и пакеты в Java
3. Объекты, классы и пакеты в Java
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 
Functor, Apply, Applicative And Monad
Functor, Apply, Applicative And MonadFunctor, Apply, Applicative And Monad
Functor, Apply, Applicative And Monad
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
 
C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84The Ring programming language version 1.2 book - Part 57 of 84
The Ring programming language version 1.2 book - Part 57 of 84
 
LinkedIn TBC JavaScript 100: Functions
 LinkedIn TBC JavaScript 100: Functions LinkedIn TBC JavaScript 100: Functions
LinkedIn TBC JavaScript 100: Functions
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
classes & objects in cpp overview
classes & objects in cpp overviewclasses & objects in cpp overview
classes & objects in cpp overview
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
 
Functions123
Functions123 Functions123
Functions123
 

Destaque

Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Virtual function
Virtual functionVirtual function
Virtual functionharman kaur
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...cprogrammings
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 

Destaque (13)

Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
OOP C++
OOP C++OOP C++
OOP C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Virtual function
Virtual functionVirtual function
Virtual function
 
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
 
functions of C++
functions of C++functions of C++
functions of C++
 
Inheritance
InheritanceInheritance
Inheritance
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 

Semelhante a Lec 45.46- virtual.functions

OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptSaadAsim11
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersBartosz Kosarzycki
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1Little Tukta Lita
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016STX Next
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchainedEduard Tomàs
 

Semelhante a Lec 45.46- virtual.functions (20)

Opp compile
Opp compileOpp compile
Opp compile
 
OBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .pptOBJECTS IN Object Oriented Programming .ppt
OBJECTS IN Object Oriented Programming .ppt
 
Lecture05
Lecture05Lecture05
Lecture05
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
ADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developersADG Poznań - Kotlin for Android developers
ADG Poznań - Kotlin for Android developers
 
New presentation oop
New presentation oopNew presentation oop
New presentation oop
 
Lecture21
Lecture21Lecture21
Lecture21
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
EcmaScript unchained
EcmaScript unchainedEcmaScript unchained
EcmaScript unchained
 
C++ programs
C++ programsC++ programs
C++ programs
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
C++11
C++11C++11
C++11
 
Lecture5
Lecture5Lecture5
Lecture5
 
Functions in C
Functions in CFunctions in C
Functions in C
 
7 functions
7  functions7  functions
7 functions
 

Mais de Princess Sam

Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-filesPrincess Sam
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointersPrincess Sam
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointersPrincess Sam
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-filesPrincess Sam
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritancePrincess Sam
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritancePrincess Sam
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 

Mais de Princess Sam (12)

Lec 50
Lec 50Lec 50
Lec 50
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointers
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointers
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-files
 
Lec 37 - pointers
Lec 37 -  pointersLec 37 -  pointers
Lec 37 - pointers
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritance
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Lec 36 - pointers
Lec 36 -  pointersLec 36 -  pointers
Lec 36 - pointers
 

Lec 45.46- virtual.functions

  • 1. Chapter: 11 Virtual Functions Lecture: 45 (Date: 06.11.2012) Lecture: 46 (Data: 07.11.2012)
  • 2. Advanced C++ Topics  Virtual functions  Friend functions  Static function  Overloaded “= ” operator  Overloaded Copy Constructor  “this” pointer
  • 3. Class (Objects, data, functions and memory)
  • 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. Class (Objects, data, functions and memory)
  • 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. Automatic Class Data class 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. Static Class Data class 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; }
  • 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. 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. 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. Advanced C++ Topics  Virtual functions  Friend functions  Static function  Overloaded “= ” operator  “this” pointer  Overloaded Copy Constructor
  • 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. 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. 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. 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. class where int main() { { private: where w1, w2, w3; char charray[10]; //occupies 10 bytes public: w1.reveal(); void reveal() w2.reveal(); { w3.reveal(); cout << “nObject’s address is “ << this; } } };
  • 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. 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(); }; }

Notas do Editor

  1. Student Book