Constructors and DestructorsObjectivesIn this lesson, you will learn to: Identify the need for constructors Declare constr...
Constructors and DestructorsObjectives (Contd.)     The call by reference method        ® Using   alias        ® Using   p...
Constructors and DestructorsConstructors Are used to initialize the member variables of the  class when the objects of the...
Constructors and DestructorsNeed for Constructors To initialize a member variable at the time of  declaration©NIIT        ...
Constructors and DestructorsDeclaration of Constructors Example:  class Calculator  {  private:     int number1, number2, ...
Constructors and DestructorsDestructors Are used to de-initialize the objects when they are  destroyed Are used to clear m...
Constructors and DestructorsNeed for Destructors To de-initialize the objects when they are destroyed To clear memory spac...
Constructors and DestructorsDeclaration of Destructors Example:  /* This Code Shows The Use Of Destructor  In The Calculat...
Constructors and DestructorsJust a Minute…Given is a code snippet for the main() function of aprogram. How would you ensur...
Constructors and DestructorsScope Resolution Operator (::) Is used to define member functions outside the class  definitio...
Constructors and DestructorsConstructors with Parameters Allow member variables of the class to be initialized  with user ...
Constructors and DestructorsConstructors with Parameters (Contd.)calculate::calculate(int x, int y){ num1=x;num2=y; total=...
Constructors and DestructorsInvoking Member Functions By using call by value By using call by reference©NIIT              ...
Constructors and DestructorsCall by Value Is useful when the function does not need to modify  the values of the original ...
Constructors and DestructorsProblem Statement 5.P.1Predict the Output:   #include iostream   void square(int);   class fun...
Constructors and DestructorsProblem Statement 5.P.1 (Contd.)    void square(int num)    {      coutnumendl;        num *= ...
Constructors and DestructorsCall by Reference Is implemented by using     An alias     Pointers©NIIT                      ...
Constructors and DestructorsCall by Reference (Contd.) Using an alias     The same variable can be referenced by more than...
Constructors and DestructorsProblem Statement 5.P.2Predict the Output:   #include iostream   void square(int );   class fu...
Constructors and DestructorsProblem Statement 5.P.2 (Contd.)    void square(int num)    {      coutnumendl;        num *= ...
Constructors and DestructorsCall by Reference using PointersTo define and declare a pointer variable: Using pointers     I...
Constructors and DestructorsCall by Reference using Pointers (Contd.) Using pointers     Allows dynamic allocation and rel...
Constructors and DestructorsSummaryIn this lesson, you learned that: Constructors are member functions of any class and  a...
Constructors and DestructorsSummary (Contd.) The data that the function must receive when called  from another function is...
Constructors and DestructorsSummary (Contd.) A pointer is a variable that stores the memory address  of another variable D...
Upcoming SlideShare
Loading in …5
×

Aae oop xp_07

577 views
481 views

Published on

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
577
On SlideShare
0
From Embeds
0
Number of Embeds
88
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Aae oop xp_07

  1. 1. Constructors and DestructorsObjectivesIn this lesson, you will learn to: Identify the need for constructors Declare constructors Identify the need for destructors Declare destructors Use scope resolution operator Use constructors with parameters Invoke member functions by using: The call by value method©NIIT OOPS/Lesson 7/Slide 1 of 25
  2. 2. Constructors and DestructorsObjectives (Contd.) The call by reference method ® Using alias ® Using pointer©NIIT OOPS/Lesson 7/Slide 2 of 25
  3. 3. Constructors and DestructorsConstructors Are used to initialize the member variables of the class when the objects of the class are created Must have the same name as that of class name Cannot return any value, not even a void type Class can have more than one constructors defined in it (known as overloaded constructors) Default constructors accept no parameters and are automatically invoked by the compiler©NIIT OOPS/Lesson 7/Slide 3 of 25
  4. 4. Constructors and DestructorsNeed for Constructors To initialize a member variable at the time of declaration©NIIT OOPS/Lesson 7/Slide 4 of 25
  5. 5. Constructors and DestructorsDeclaration of Constructors Example: class Calculator { private: int number1, number2, tot; public: ... Calculator() { number1 = number2 = tot = 0; cout Constructor invoked endl; } };©NIIT OOPS/Lesson 7/Slide 5 of 25
  6. 6. Constructors and DestructorsDestructors Are used to de-initialize the objects when they are destroyed Are used to clear memory space occupied by a data member when an object goes out of scope Must have the same name as that of the class, preceded by a ~ (For example: ~Calculator()) Are automatically invoked Can also be explicitly invoked when required Cannot be overloaded©NIIT OOPS/Lesson 7/Slide 6 of 25
  7. 7. Constructors and DestructorsNeed for Destructors To de-initialize the objects when they are destroyed To clear memory space occupied by a data member when an object goes out of scope©NIIT OOPS/Lesson 7/Slide 7 of 25
  8. 8. Constructors and DestructorsDeclaration of Destructors Example: /* This Code Shows The Use Of Destructor In The Calculator Class */ class Calculator { private: int number1, number2, tot; public: ... ~Calculator() //Body Of The //Destructor { number1 = number2 = tot = 0; } };©NIIT OOPS/Lesson 7/Slide 8 of 25
  9. 9. Constructors and DestructorsJust a Minute…Given is a code snippet for the main() function of aprogram. How would you ensure that the following tasksare accomplished when the program is executed: 1. The member variables are initialized with zero 2. On exit or termination of the program the following message is displayed : “Bye Folks!!! Have a Nice Time” #include iostream int main() { Number num1;num1.disp(); } Hint: The Number class has only one membervariable©NIIT myNum. OOPS/Lesson 7/Slide 9 of 25
  10. 10. Constructors and DestructorsScope Resolution Operator (::) Is used to define member functions outside the class definition therefore making the class definition more readable Example: class calculator { public: void input(); }; void calculator::input () {…}©NIIT OOPS/Lesson 7/Slide 10 of 25
  11. 11. Constructors and DestructorsConstructors with Parameters Allow member variables of the class to be initialized with user supplied values from main() function also called parametersExample class calculate { private: int num1,num2,total; public: calculate(int, int); };©NIIT OOPS/Lesson 7/Slide 11 of 25
  12. 12. Constructors and DestructorsConstructors with Parameters (Contd.)calculate::calculate(int x, int y){ num1=x;num2=y; total=0;} int main(){//Accept values in two variable var1 //var2calculate c1(var1,var2);}©NIIT OOPS/Lesson 7/Slide 12 of 25
  13. 13. Constructors and DestructorsInvoking Member Functions By using call by value By using call by reference©NIIT OOPS/Lesson 7/Slide 13 of 25
  14. 14. Constructors and DestructorsCall by Value Is useful when the function does not need to modify the values of the original variables Does not affect the values of the variables in caller functions©NIIT OOPS/Lesson 7/Slide 14 of 25
  15. 15. Constructors and DestructorsProblem Statement 5.P.1Predict the Output: #include iostream void square(int); class functionCall { int number; public: functionCall(); }; functionCall::functionCall() { number=10;square(number); }©NIIT OOPS/Lesson 7/Slide 15 of 25
  16. 16. Constructors and DestructorsProblem Statement 5.P.1 (Contd.) void square(int num) { coutnumendl; num *= num; //This Expression Is //Resolved As num = num * num cout num endl; } int main() { functionCall f1; return 0; }©NIIT OOPS/Lesson 7/Slide 16 of 25
  17. 17. Constructors and DestructorsCall by Reference Is implemented by using An alias Pointers©NIIT OOPS/Lesson 7/Slide 17 of 25
  18. 18. Constructors and DestructorsCall by Reference (Contd.) Using an alias The same variable can be referenced by more than one name by using the or the alias operator The change in the value of the variable by the called or the calling program is reflected in all the affected functions©NIIT OOPS/Lesson 7/Slide 18 of 25
  19. 19. Constructors and DestructorsProblem Statement 5.P.2Predict the Output: #include iostream void square(int ); class functionCall { int number; public: functionCall(); }; functionCall::functionCall() { number=10;square(number); }©NIIT OOPS/Lesson 7/Slide 19 of 25
  20. 20. Constructors and DestructorsProblem Statement 5.P.2 (Contd.) void square(int num) { coutnumendl; num *= num; //This Expression Is //Resolved As num = num * num cout num endl; } int main() { functionCall f1; return 0; }©NIIT OOPS/Lesson 7/Slide 20 of 25
  21. 21. Constructors and DestructorsCall by Reference using PointersTo define and declare a pointer variable: Using pointers Involves a pointer variable storing the memory address of any variable Is advantageous since it allows direct access to individual bytes in the memory and output devices Example: char var = G; char *ptr ; //Pointer Declaration ptr = var; //Stores the address of //the variable©NIIT OOPS/Lesson 7/Slide 21 of 25
  22. 22. Constructors and DestructorsCall by Reference using Pointers (Contd.) Using pointers Allows dynamic allocation and release of memory that is program can obtain memory while it is running by using new and release by using delete operator Syntax: variable = new type; The type of variable mentioned on the left hand and the type mentioned on the right side of the new operator should match delete variable©NIIT OOPS/Lesson 7/Slide 22 of 25
  23. 23. Constructors and DestructorsSummaryIn this lesson, you learned that: Constructors are member functions of any class and are invoked the moment an instance of the class to which they belong is created A constructor function has the same name as its class A destructor function is invoked when any instance of a class ceases to exist A destructor function has the same name as its class but prefixed with a ~ (tilde) The member functions and the constructors of a class can also be defined outside the boundary of the class, using the scope resolution operator (::)©NIIT OOPS/Lesson 7/Slide 23 of 25
  24. 24. Constructors and DestructorsSummary (Contd.) The data that the function must receive when called from another function is/are called the parameters of a function In C++ programs, functions that have parameters are invoked in one of the following ways: A call by value A call by reference You can give two names to a variable by using the alias operator A reference provides an alias, or an alternate name for the variable©NIIT OOPS/Lesson 7/Slide 24 of 25
  25. 25. Constructors and DestructorsSummary (Contd.) A pointer is a variable that stores the memory address of another variable Dynamic allocation is the means by which a program can obtain memory while it is running The capability of obtaining memory as the need arises is provided by the new operator The delete operator is used to release memory©NIIT OOPS/Lesson 7/Slide 25 of 25

×