SlideShare a Scribd company logo
OOPs & C++ UNIT 3
BY:SURBHI SAROHA
SYLLABUS
 Class
 Object
 Constructor & Destructor
 Class Modifiers(Private, Public & Protected)
 Data Member
 Member Function
 Static Data Member
 Static Member Function
 Friend Function
Cont….
 Object
 Constructor(Default Constructor , Parameterized Constructor and Copy
Constructor)
 Destructor
Class
 C++ is an object-oriented programming language.
 Everything in C++ is associated with classes and objects, along with its
attributes and methods.
 For example: in real life, a car is an object.
 The car has attributes, such as weight and color, and methods, such as drive
and brake.
 Attributes and methods are basically variables and functions that belongs to
the class.
 These are often referred to as "class members".
 A class is a user-defined data type that we can use in our program, and it
works as an object constructor, or a "blueprint" for creating objects.
Create a Class
 To create a class, use the class keyword:
 Example
 Create a class called "MyClass":
 class MyClass { // The class
 public: // Access specifier
 int myNum; // Attribute (int variable)
 string myString; // Attribute (string variable)
 };
Create an Object
 In C++, an object is created from a class. We have already created the class named MyClass,
so now we can use this to create objects.
 To create an object of MyClass, specify the class name, followed by the object name.
 To access the class attributes (myNum and myString), use the dot syntax (.) on the object:
 Example
 Create an object called "myObj" and access the attributes:
 class MyClass { // The class
 public: // Access specifier
Cont….
 int myNum; // Attribute (int variable)
 string myString; // Attribute (string variable)
 };
 int main() {
 MyClass myObj; // Create an object of MyClass
 // Access attributes and set values

Cont….
 myObj.myNum = 15;
 myObj.myString = "Some text";
 // Print attribute values
 cout << myObj.myNum << "n";
 cout << myObj.myString;
 return 0;
 }
Constructor & Destructor
 A constructor is a member function of a class that has the same name as the
class name.
 It helps to initialize the object of a class.
 It can either accept the arguments or not.
 It is used to allocate the memory to an object of the class.
 It is called whenever an instance of the class is created.
 It can be defined manually with arguments or without arguments.
 There can be many constructors in a class.
 It can be overloaded but it can not be inherited or virtual.
 There is a concept of copy constructor which is used to initialize an object
from another object.
Syntax:
 ClassName()
 {
 //Constructor's Body
 }
Destructor
 Like a constructor, Destructor is also a member function of a class that has
the same name as the class name preceded by a tilde(~) operator.
 It helps to deallocate the memory of an object.
 It is called while the object of the class is freed or deleted.
 In a class, there is always a single destructor without any parameters so it
can’t be overloaded.
 It is always called in the reverse order of the constructor.
 if a class is inherited by another class and both the classes have a destructor
then the destructor of the child class is called first, followed by the
destructor of the parent or base class.
Destructor
 ~ClassName()
 {
 //Destructor's Body
 }
Class Modifiers(Private, Public &
Protected)
 In C++, there are three access specifiers:
 public - members are accessible from outside the class
 private - members cannot be accessed (or viewed) from outside the class
 protected - members cannot be accessed from outside the class, however,
they can be accessed in inherited classes.
Example
 #include <iostream>
 using namespace std;
 class MyClass {
 public: // Public access specifier
 int x; // Public attribute
 private: // Private access specifier
 int y; // Private attribute
 };
Cont….
 int main() {
 MyClass myObj;
 myObj.x = 25; // Allowed (x is public)
 myObj.y = 50; // Not allowed (y is private)
 return 0;
 }
Data Member
 Data members include members that are declared with any of the fundamental
types, as well as other types, including pointer, reference, array types, bit fields,
and user-defined types.
 You can declare a data member the same way as a variable, except that explicit
initializers are not allowed inside the class definition.
 However, a const static data member of integral or enumeration type may have an
explicit initializer.
 If an array is declared as a nonstatic class member, you must specify all of the
dimensions of the array.
 A class can have members that are of a class type or are pointers or references to
a class type. Members that are of a class type must be of a class type that has
been previously declared. An incomplete class type can be used in a member
declaration as long as the size of the class is not needed. For example, a member
can be declared that is a pointer to an incomplete class type.
Member Function
 Member functions are operators and functions that are declared as members of a
class.
 Member functions do not include operators and functions declared with the friend
specifier.
 These are called friends of a class.
 You can declare a member function as static; this is called a static member
function.
 A member function that is not declared as static is called a nonstatic member
function.
 class x { public: int add() // inline member function
 add {return a+b+c;};
 private: int a,b,c;
 };
Static Data Member
 Static data members are class members that are declared
using static keywords. A static member has certain special characteristics
which are as follows:
 Only one copy of that member is created for the entire class and is shared by
all the objects of that class, no matter how many objects are created.
 It is initialized before any object of this class is created, even before the
main starts.
 It is visible only within the class, but its lifetime is the entire program.
 Syntax:
 static data_type data_member_name;
// C++ Program to demonstrate
// the working of static data member
 #include <iostream>
 using namespace std;
 class A {
 public:
 A()
 {
 cout << "A's Constructor Called " <<
 endl;
 }
 };

Cont….
 class B {
 static A a;

 public:
 B()
 {
 cout << "B's Constructor Called " <<
 endl;
 }
 };

Cont….
 // Driver code
 int main()
 {
 B b;
 return 0;
 }
Static Member Function
 Static Member Function in a class is the function that is declared as static
because of which function attains certain properties as defined below:
 A static member function is independent of any object of the class.
 A static member function can be called even if no objects of the class exist.
 A static member function can also be accessed using the class name through
the scope resolution operator.
 A static member function can access static data members and static member
functions inside or outside of the class.
 Static member functions have a scope inside the class and cannot access the
current object pointer.
 You can also use a static member function to determine how many objects of
the class have been created.
// C++ Program to show the working of
// static member functions
 #include <iostream>
 using namespace std;

 class Box
 {
 private:
 static int length;
 static int breadth;
 static int height;

 public:

Cont….
 static void print()
 {
 cout << "The value of the length is: " << length << endl;
 cout << "The value of the breadth is: " << breadth << endl;
 cout << "The value of the height is: " << height << endl;
 }
 };
Cont….
 // initialize the static data members
 int Box :: length = 10;
 int Box :: breadth = 20;
 int Box :: height = 30;

 // Driver Code

 int main()
 {

 Box b;

Cont….
 cout << "Static member function is called through Object name: n" << endl;
 b.print();

 cout << "nStatic member function is called through Class name: n" <<
endl;
 Box::print();

 return 0;
 }
Output
 Static member function is called through Object name:
 The value of the length is: 10
 The value of the breadth is: 20
 The value of the height is: 30
 Static member function is called through Class name:
 The value of the length is: 10
 The value of the breadth is: 20
 The value of the height is: 30
Friend Function
 A friend class can access private and protected members of other classes in
which it is declared as a friend.
 It is sometimes useful to allow a particular class to access private and
protected members of other classes.
 For example, a LinkedList class may be allowed to access private members of
Node.
 We can declare a friend class in C++ by using the friend keyword.
 Syntax:
 friend class class_name; // declared in the base class
Cont…..
 If a function is defined as a friend function in C++, then the protected and
private data of a class can be accessed using the function.
 By using the keyword friend compiler knows the given function is a friend
function.
 For accessing the data, the declaration of a friend function should be done
inside the body of a class starting with the keyword friend.
Advantages of Friend Function in C++
 The friend function allows the programmer to generate more efficient codes.
 It allows the sharing of private class information by a non-member function.
 It accesses the non-public members of a class easily.
 It is widely used in cases when two or more classes contain the interrelated
members relative to other parts of the program.
 It allows additional functionality that is not used by the class commonly.
Example
 #include <iostream>
 using namespace std;
 class Distance {
 private:
 int meter;
 // friend function
 friend int addFive(Distance);
 public:
Cont….
 Distance() : meter(0) {}
 };
 // friend function definition
 int addFive(Distance d) {
 //accessing private members from the friend function
 d.meter += 5;
 return d.meter;
 }
 int main() {
Cont….
 Distance D;
 cout << "Distance: " << addFive(D);
 return 0;
 }
Object
 In C++, Object is a real world entity, for example, chair, car, pen, mobile,
laptop etc.
 In other words, object is an entity that has state and behavior.
 Here, state means data and behavior means functionality.
 Object is a runtime entity, it is created at runtime.
Constructor(Default Constructor ,
Parameterized Constructor and Copy
Constructor)
 A constructor in C++ is a special member function with exact same name as
the class name.
 The constructor name is the same as the Class Name. Reason: Compiler uses
this character to differentiate constructors from the other member functions
of the class.
 A constructor must not declare a return type or void. Reason: As it’s
automatically called and generally used for initializing values.
 They can be defined inside or outside the class definition.
 Automatically calls when an object is created for the class.
 It uses to construct that means to initialize all the data members (variables)
of the class.
Constructors types in C++
 1. Default Constructor
 A constructor with no arguments (or parameters) in the definition is a
default constructor.
 It is the type of constructor in C++ usually used to initialize data members
(variables) with real values.
 Note: The compiler automatically creates a default constructor without data
member (variables) or initialization if no constructor is explicitly declared.
Default Constructor
 #include <iostream>
 using namespace std;

 //Class Name: Default_construct
 class Default_construct
 {
 public:
 int a, b;

 // Default Constructor
 Default_construct()
 {

Cont….
 a = 100;
 b = 200;
 }
 };

 int main()
 {
 Default_construct con; //Object created
 cout << "Value of a: " << con.a;
 cout<< "Value of b: " << con.b;
 return 0;
 }
2. Parameterized Constructor
 Unlike the Default constructor, It contains parameters (or arguments) in the constructor
definition and declaration.
 More than one argument can also pass through a parameterized constructor.
 #include <iostream>
 using namespace std;

 // class name: Rectangle
 class Rectangle {
 private:
 double length;
 double breadth;

 public:
Cont….
 // parameterized constructor
 Rectangle(double l, double b) {
 length = l;
 breadth = b;
 }

 double calculateArea() {
 return length * breadth;
 }
 };

 int main() {
 // create objects to call constructors
Cont….
 Rectangle obj1(10,6);
 Rectangle obj2(13,8);

 cout << "Area of Rectangle 1: " << obj1.calculateArea();
 cout << "Area of Rectangle 2: " << obj2.calculateArea();

 return 0;
 }
3. Copy Constructor
 A copy constructor is the third type among various types of constructors in
C++.
 The member function initializes an object using another object of the same
class.
 It helps to copy data from one object to another.
Example
 #include <iostream>
 using namespace std;
 // class name: Rectangle
 class Rectangle {
 private:
 double length;
 double breadth;
 public:
 // parameterized constructor
 Rectangle(double l, double b) {
 length = l;
 breadth = b;
 }
Cont….
 // copy constructor with a Rectangle object as parameter copies data of the obj parameter
 Rectangle(Rectangle &obj) {
 length = obj.length;
 breadth = obj.breadth;
 }


 double calculateArea() {
 return length * breadth;
 }
 };

Cont….
 int main() {
 // create objects to call constructors
 Rectangle obj1(10,6);
 Rectangle obj2 = obj1; //copy the content using object

 //print areas of rectangles
 cout << "Area of Rectangle 1: " << obj1.calculateArea();
 cout << "Area of Rectangle 2: " << obj2.calculateArea();

 return 0;
 }
Destructor
 #include <iostream>
 using namespace std;
 class Employee
 {
 public:
 Employee()
 {
 cout<<"Constructor Invoked"<<endl;
 }

Cont…..
 ~Employee()
 {
 cout<<"Destructor Invoked"<<endl;
 }
 };
 int main(void)
 {
 Employee e1; //creating an object of Employee
 Employee e2; //creating an object of Employee
 return 0;
 }
Thank you 

More Related Content

PPTX
User defined function in c
PPTX
Member Function in C++
DOCX
Encapsulation in C++
PDF
Python programming : Inheritance and polymorphism
PPT
Structure in c
PDF
Orientação a objetos em Python (compacto)
PDF
Object oriented approach in python programming
PPTX
20.3 Java encapsulation
User defined function in c
Member Function in C++
Encapsulation in C++
Python programming : Inheritance and polymorphism
Structure in c
Orientação a objetos em Python (compacto)
Object oriented approach in python programming
20.3 Java encapsulation

What's hot (20)

PPT
Arrays in c
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
PPT
Input and output in C++
PPT
C++ Arrays
PPTX
virtual function
PPTX
Inline function
PPTX
592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx
PPTX
Python: Basic Inheritance
PPTX
classes and objects in C++
PPTX
Standard template library
PPTX
Python – Object Oriented Programming
PPTX
Constructor and desturctor
PPT
C++ Returning Objects
PPTX
Lecture_7-Encapsulation in Java.pptx
PDF
Constructor and Destructor
PPTX
OOP C++
PPTX
Typedef
PDF
Class and object
PDF
Strings IN C
Arrays in c
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
Input and output in C++
C++ Arrays
virtual function
Inline function
592047465-4-Const-vs-Non-const-Functions-Amd-Static-Data-Members-Functions.pptx
Python: Basic Inheritance
classes and objects in C++
Standard template library
Python – Object Oriented Programming
Constructor and desturctor
C++ Returning Objects
Lecture_7-Encapsulation in Java.pptx
Constructor and Destructor
OOP C++
Typedef
Class and object
Strings IN C
Ad

Similar to OOPs & C++ UNIT 3 (20)

PPTX
class c++
PPTX
Class and object
PPT
classes data type for Btech students.ppt
PDF
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
PDF
PDF
Class and object in C++ By Pawan Thakur
PPT
Classes, objects and methods
PPTX
PDF
Object Oriented Programming using C++ - Part 2
PPTX
Lecture 2 (1)
PDF
chapter-7-classes-and-objects.pdf
PPTX
Opp concept in c++
PPSX
Arre tari mano bhosko ka pikina tari ma no piko
PDF
Classes-and-Objects-in-C++.pdf
PPT
Object and class presentation
PPTX
05 Object Oriented Concept Presentation.pptx
PPT
Unit vi(dsc++)
PDF
Chapter22 static-class-member-example
PPT
Class and object in C++
PPTX
Lecture 4.2 c++(comlete reference book)
class c++
Class and object
classes data type for Btech students.ppt
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
Class and object in C++ By Pawan Thakur
Classes, objects and methods
Object Oriented Programming using C++ - Part 2
Lecture 2 (1)
chapter-7-classes-and-objects.pdf
Opp concept in c++
Arre tari mano bhosko ka pikina tari ma no piko
Classes-and-Objects-in-C++.pdf
Object and class presentation
05 Object Oriented Concept Presentation.pptx
Unit vi(dsc++)
Chapter22 static-class-member-example
Class and object in C++
Lecture 4.2 c++(comlete reference book)
Ad

More from Dr. SURBHI SAROHA (20)

PPTX
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
PPTX
MOBILE COMPUTING UNIT 2 by surbhi saroha
PPTX
Mobile Computing UNIT 1 by surbhi saroha
PPTX
DEEP LEARNING (UNIT 2 ) by surbhi saroha
PPTX
Introduction to Deep Leaning(UNIT 1).pptx
PPTX
Cloud Computing (Infrastructure as a Service)UNIT 2
PPTX
Management Information System(Unit 2).pptx
PPTX
Searching in Data Structure(Linear search and Binary search)
PPTX
Management Information System(UNIT 1).pptx
PPTX
Introduction to Cloud Computing(UNIT 1).pptx
PPTX
JAVA (UNIT 5)
PPTX
DBMS (UNIT 5)
PPTX
DBMS UNIT 4
PPTX
JAVA(UNIT 4)
PPTX
OOPs & C++(UNIT 5)
PPTX
OOPS & C++(UNIT 4)
PPTX
DBMS UNIT 3
PPTX
JAVA (UNIT 3)
PPTX
Keys in dbms(UNIT 2)
PPTX
DBMS (UNIT 2)
Deep learning(UNIT 3) BY Ms SURBHI SAROHA
MOBILE COMPUTING UNIT 2 by surbhi saroha
Mobile Computing UNIT 1 by surbhi saroha
DEEP LEARNING (UNIT 2 ) by surbhi saroha
Introduction to Deep Leaning(UNIT 1).pptx
Cloud Computing (Infrastructure as a Service)UNIT 2
Management Information System(Unit 2).pptx
Searching in Data Structure(Linear search and Binary search)
Management Information System(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
JAVA (UNIT 5)
DBMS (UNIT 5)
DBMS UNIT 4
JAVA(UNIT 4)
OOPs & C++(UNIT 5)
OOPS & C++(UNIT 4)
DBMS UNIT 3
JAVA (UNIT 3)
Keys in dbms(UNIT 2)
DBMS (UNIT 2)

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Institutional Correction lecture only . . .
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Insiders guide to clinical Medicine.pdf
PDF
Classroom Observation Tools for Teachers
PDF
01-Introduction-to-Information-Management.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
master seminar digital applications in india
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Cell Structure & Organelles in detailed.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Final Presentation General Medicine 03-08-2024.pptx
Institutional Correction lecture only . . .
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
O7-L3 Supply Chain Operations - ICLT Program
Insiders guide to clinical Medicine.pdf
Classroom Observation Tools for Teachers
01-Introduction-to-Information-Management.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
RMMM.pdf make it easy to upload and study
human mycosis Human fungal infections are called human mycosis..pptx
master seminar digital applications in india
PPH.pptx obstetrics and gynecology in nursing
Week 4 Term 3 Study Techniques revisited.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Cell Structure & Organelles in detailed.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

OOPs & C++ UNIT 3

  • 1. OOPs & C++ UNIT 3 BY:SURBHI SAROHA
  • 2. SYLLABUS  Class  Object  Constructor & Destructor  Class Modifiers(Private, Public & Protected)  Data Member  Member Function  Static Data Member  Static Member Function  Friend Function
  • 3. Cont….  Object  Constructor(Default Constructor , Parameterized Constructor and Copy Constructor)  Destructor
  • 4. Class  C++ is an object-oriented programming language.  Everything in C++ is associated with classes and objects, along with its attributes and methods.  For example: in real life, a car is an object.  The car has attributes, such as weight and color, and methods, such as drive and brake.  Attributes and methods are basically variables and functions that belongs to the class.  These are often referred to as "class members".  A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects.
  • 5. Create a Class  To create a class, use the class keyword:  Example  Create a class called "MyClass":  class MyClass { // The class  public: // Access specifier  int myNum; // Attribute (int variable)  string myString; // Attribute (string variable)  };
  • 6. Create an Object  In C++, an object is created from a class. We have already created the class named MyClass, so now we can use this to create objects.  To create an object of MyClass, specify the class name, followed by the object name.  To access the class attributes (myNum and myString), use the dot syntax (.) on the object:  Example  Create an object called "myObj" and access the attributes:  class MyClass { // The class  public: // Access specifier
  • 7. Cont….  int myNum; // Attribute (int variable)  string myString; // Attribute (string variable)  };  int main() {  MyClass myObj; // Create an object of MyClass  // Access attributes and set values 
  • 8. Cont….  myObj.myNum = 15;  myObj.myString = "Some text";  // Print attribute values  cout << myObj.myNum << "n";  cout << myObj.myString;  return 0;  }
  • 9. Constructor & Destructor  A constructor is a member function of a class that has the same name as the class name.  It helps to initialize the object of a class.  It can either accept the arguments or not.  It is used to allocate the memory to an object of the class.  It is called whenever an instance of the class is created.  It can be defined manually with arguments or without arguments.  There can be many constructors in a class.  It can be overloaded but it can not be inherited or virtual.  There is a concept of copy constructor which is used to initialize an object from another object.
  • 10. Syntax:  ClassName()  {  //Constructor's Body  }
  • 11. Destructor  Like a constructor, Destructor is also a member function of a class that has the same name as the class name preceded by a tilde(~) operator.  It helps to deallocate the memory of an object.  It is called while the object of the class is freed or deleted.  In a class, there is always a single destructor without any parameters so it can’t be overloaded.  It is always called in the reverse order of the constructor.  if a class is inherited by another class and both the classes have a destructor then the destructor of the child class is called first, followed by the destructor of the parent or base class.
  • 12. Destructor  ~ClassName()  {  //Destructor's Body  }
  • 13. Class Modifiers(Private, Public & Protected)  In C++, there are three access specifiers:  public - members are accessible from outside the class  private - members cannot be accessed (or viewed) from outside the class  protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
  • 14. Example  #include <iostream>  using namespace std;  class MyClass {  public: // Public access specifier  int x; // Public attribute  private: // Private access specifier  int y; // Private attribute  };
  • 15. Cont….  int main() {  MyClass myObj;  myObj.x = 25; // Allowed (x is public)  myObj.y = 50; // Not allowed (y is private)  return 0;  }
  • 16. Data Member  Data members include members that are declared with any of the fundamental types, as well as other types, including pointer, reference, array types, bit fields, and user-defined types.  You can declare a data member the same way as a variable, except that explicit initializers are not allowed inside the class definition.  However, a const static data member of integral or enumeration type may have an explicit initializer.  If an array is declared as a nonstatic class member, you must specify all of the dimensions of the array.  A class can have members that are of a class type or are pointers or references to a class type. Members that are of a class type must be of a class type that has been previously declared. An incomplete class type can be used in a member declaration as long as the size of the class is not needed. For example, a member can be declared that is a pointer to an incomplete class type.
  • 17. Member Function  Member functions are operators and functions that are declared as members of a class.  Member functions do not include operators and functions declared with the friend specifier.  These are called friends of a class.  You can declare a member function as static; this is called a static member function.  A member function that is not declared as static is called a nonstatic member function.  class x { public: int add() // inline member function  add {return a+b+c;};  private: int a,b,c;  };
  • 18. Static Data Member  Static data members are class members that are declared using static keywords. A static member has certain special characteristics which are as follows:  Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.  It is initialized before any object of this class is created, even before the main starts.  It is visible only within the class, but its lifetime is the entire program.  Syntax:  static data_type data_member_name;
  • 19. // C++ Program to demonstrate // the working of static data member  #include <iostream>  using namespace std;  class A {  public:  A()  {  cout << "A's Constructor Called " <<  endl;  }  }; 
  • 20. Cont….  class B {  static A a;   public:  B()  {  cout << "B's Constructor Called " <<  endl;  }  }; 
  • 21. Cont….  // Driver code  int main()  {  B b;  return 0;  }
  • 22. Static Member Function  Static Member Function in a class is the function that is declared as static because of which function attains certain properties as defined below:  A static member function is independent of any object of the class.  A static member function can be called even if no objects of the class exist.  A static member function can also be accessed using the class name through the scope resolution operator.  A static member function can access static data members and static member functions inside or outside of the class.  Static member functions have a scope inside the class and cannot access the current object pointer.  You can also use a static member function to determine how many objects of the class have been created.
  • 23. // C++ Program to show the working of // static member functions  #include <iostream>  using namespace std;   class Box  {  private:  static int length;  static int breadth;  static int height;   public: 
  • 24. Cont….  static void print()  {  cout << "The value of the length is: " << length << endl;  cout << "The value of the breadth is: " << breadth << endl;  cout << "The value of the height is: " << height << endl;  }  };
  • 25. Cont….  // initialize the static data members  int Box :: length = 10;  int Box :: breadth = 20;  int Box :: height = 30;   // Driver Code   int main()  {   Box b; 
  • 26. Cont….  cout << "Static member function is called through Object name: n" << endl;  b.print();   cout << "nStatic member function is called through Class name: n" << endl;  Box::print();   return 0;  }
  • 27. Output  Static member function is called through Object name:  The value of the length is: 10  The value of the breadth is: 20  The value of the height is: 30  Static member function is called through Class name:  The value of the length is: 10  The value of the breadth is: 20  The value of the height is: 30
  • 28. Friend Function  A friend class can access private and protected members of other classes in which it is declared as a friend.  It is sometimes useful to allow a particular class to access private and protected members of other classes.  For example, a LinkedList class may be allowed to access private members of Node.  We can declare a friend class in C++ by using the friend keyword.  Syntax:  friend class class_name; // declared in the base class
  • 29. Cont…..  If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function.  By using the keyword friend compiler knows the given function is a friend function.  For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.
  • 30. Advantages of Friend Function in C++  The friend function allows the programmer to generate more efficient codes.  It allows the sharing of private class information by a non-member function.  It accesses the non-public members of a class easily.  It is widely used in cases when two or more classes contain the interrelated members relative to other parts of the program.  It allows additional functionality that is not used by the class commonly.
  • 31. Example  #include <iostream>  using namespace std;  class Distance {  private:  int meter;  // friend function  friend int addFive(Distance);  public:
  • 32. Cont….  Distance() : meter(0) {}  };  // friend function definition  int addFive(Distance d) {  //accessing private members from the friend function  d.meter += 5;  return d.meter;  }  int main() {
  • 33. Cont….  Distance D;  cout << "Distance: " << addFive(D);  return 0;  }
  • 34. Object  In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.  In other words, object is an entity that has state and behavior.  Here, state means data and behavior means functionality.  Object is a runtime entity, it is created at runtime.
  • 35. Constructor(Default Constructor , Parameterized Constructor and Copy Constructor)  A constructor in C++ is a special member function with exact same name as the class name.  The constructor name is the same as the Class Name. Reason: Compiler uses this character to differentiate constructors from the other member functions of the class.  A constructor must not declare a return type or void. Reason: As it’s automatically called and generally used for initializing values.  They can be defined inside or outside the class definition.  Automatically calls when an object is created for the class.  It uses to construct that means to initialize all the data members (variables) of the class.
  • 36. Constructors types in C++  1. Default Constructor  A constructor with no arguments (or parameters) in the definition is a default constructor.  It is the type of constructor in C++ usually used to initialize data members (variables) with real values.  Note: The compiler automatically creates a default constructor without data member (variables) or initialization if no constructor is explicitly declared.
  • 37. Default Constructor  #include <iostream>  using namespace std;   //Class Name: Default_construct  class Default_construct  {  public:  int a, b;   // Default Constructor  Default_construct()  { 
  • 38. Cont….  a = 100;  b = 200;  }  };   int main()  {  Default_construct con; //Object created  cout << "Value of a: " << con.a;  cout<< "Value of b: " << con.b;  return 0;  }
  • 39. 2. Parameterized Constructor  Unlike the Default constructor, It contains parameters (or arguments) in the constructor definition and declaration.  More than one argument can also pass through a parameterized constructor.  #include <iostream>  using namespace std;   // class name: Rectangle  class Rectangle {  private:  double length;  double breadth;   public:
  • 40. Cont….  // parameterized constructor  Rectangle(double l, double b) {  length = l;  breadth = b;  }   double calculateArea() {  return length * breadth;  }  };   int main() {  // create objects to call constructors
  • 41. Cont….  Rectangle obj1(10,6);  Rectangle obj2(13,8);   cout << "Area of Rectangle 1: " << obj1.calculateArea();  cout << "Area of Rectangle 2: " << obj2.calculateArea();   return 0;  }
  • 42. 3. Copy Constructor  A copy constructor is the third type among various types of constructors in C++.  The member function initializes an object using another object of the same class.  It helps to copy data from one object to another.
  • 43. Example  #include <iostream>  using namespace std;  // class name: Rectangle  class Rectangle {  private:  double length;  double breadth;  public:  // parameterized constructor  Rectangle(double l, double b) {  length = l;  breadth = b;  }
  • 44. Cont….  // copy constructor with a Rectangle object as parameter copies data of the obj parameter  Rectangle(Rectangle &obj) {  length = obj.length;  breadth = obj.breadth;  }    double calculateArea() {  return length * breadth;  }  }; 
  • 45. Cont….  int main() {  // create objects to call constructors  Rectangle obj1(10,6);  Rectangle obj2 = obj1; //copy the content using object   //print areas of rectangles  cout << "Area of Rectangle 1: " << obj1.calculateArea();  cout << "Area of Rectangle 2: " << obj2.calculateArea();   return 0;  }
  • 46. Destructor  #include <iostream>  using namespace std;  class Employee  {  public:  Employee()  {  cout<<"Constructor Invoked"<<endl;  } 
  • 47. Cont…..  ~Employee()  {  cout<<"Destructor Invoked"<<endl;  }  };  int main(void)  {  Employee e1; //creating an object of Employee  Employee e2; //creating an object of Employee  return 0;  }