SlideShare a Scribd company logo
1 of 48
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

Similar to OOPs & C++ UNIT 3

chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfismartshanker1
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-exampleDeepak Singh
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Abu Saleh
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++Swarup Boro
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsRai University
 

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

Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)Lecture 4.2 c++(comlete reference book)
Lecture 4.2 c++(comlete reference book)
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
C++ classes
C++ classesC++ classes
C++ classes
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 

More from SURBHI SAROHA

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2SURBHI SAROHA
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptxSURBHI SAROHA
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)SURBHI SAROHA
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxSURBHI SAROHA
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxSURBHI SAROHA
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1SURBHI SAROHA
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)SURBHI SAROHA
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)SURBHI SAROHA
 

More from SURBHI SAROHA (20)

Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2Cloud Computing (Infrastructure as a Service)UNIT 2
Cloud Computing (Infrastructure as a Service)UNIT 2
 
Management Information System(Unit 2).pptx
Management Information System(Unit 2).pptxManagement Information System(Unit 2).pptx
Management Information System(Unit 2).pptx
 
Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)Searching in Data Structure(Linear search and Binary search)
Searching in Data Structure(Linear search and Binary search)
 
Management Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptxManagement Information System(UNIT 1).pptx
Management Information System(UNIT 1).pptx
 
Introduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptxIntroduction to Cloud Computing(UNIT 1).pptx
Introduction to Cloud Computing(UNIT 1).pptx
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
DBMS (UNIT 5)
DBMS (UNIT 5)DBMS (UNIT 5)
DBMS (UNIT 5)
 
DBMS UNIT 4
DBMS UNIT 4DBMS UNIT 4
DBMS UNIT 4
 
JAVA(UNIT 4)
JAVA(UNIT 4)JAVA(UNIT 4)
JAVA(UNIT 4)
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)OOPS & C++(UNIT 4)
OOPS & C++(UNIT 4)
 
DBMS UNIT 3
DBMS UNIT 3DBMS UNIT 3
DBMS UNIT 3
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)Keys in dbms(UNIT 2)
Keys in dbms(UNIT 2)
 
DBMS (UNIT 2)
DBMS (UNIT 2)DBMS (UNIT 2)
DBMS (UNIT 2)
 
JAVA UNIT 2
JAVA UNIT 2JAVA UNIT 2
JAVA UNIT 2
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1Object-Oriented Programming with Java UNIT 1
Object-Oriented Programming with Java UNIT 1
 
Database Management System(UNIT 1)
Database Management System(UNIT 1)Database Management System(UNIT 1)
Database Management System(UNIT 1)
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 

Recently uploaded

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

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;  }