SlideShare a Scribd company logo
Unit   ii
Structure Definitions
struct time
{
int hour;
int min;
int second;
}
Accessing members of structure
time t;
cout<<t.hour;
time * timeptr;
Using pointers
timeptr = &t;
cout<<timeptr->hour;
(*timeptr).hour;
Example
Classes and Objects
 A class can be described as a collection of data
members and member functions.
 Functions are called member functions and define a
set of operations that can be performed on the data
members of the class.
 The association of data and member functions are
called encapsulation.
Unit   ii
Class objects
 The process of creating objects(variables) of the class
is called class instantiation. Necessary resources are
allocated only when the class is instantiated.
 The syntax is as follows:
 Class className ObjectName…..
 Ex: class student s1;
Accessing Class members
 Class members are accessed using member access
operator , dot(.)
 Syntax : ObjectName.Datamember;
 Example: Student s1;
 s1.rollno;
 s1.printdetails();
Defining member functions
 Member functions of the class can be defined in any
one of the following ways
 Inside the class specification
 Outside the class specification
Member functions outside the
class body
Example
Access specifiers
 Private: The private members of a class have strict access
control. Only the member functions of the same class can access
these members. It prevents accidental modifications of the data
members.
class person
{
private:
int age;
int getage();
};
Person p1;
p1.age = 10; // cannot access private data
p1.getage(); // cannot access private function
Public members
 The members of a class which are visible outside the class
are declared in public section.
class person
{
public:
int age;
int getage();
};
Person p1;
p1.age = 10; // can access public datat
p1.getage(); // can access public function
Protected members
 The access control of protected members is similar to
private members and has significance to inheritance.
 Empty classes;
 Class xyz{};
 Class Empty {};
Constructors and Destructors
 Constructors:
 The constructor gets called automatically for each
object that has just got created.
 It appears as member function of each class, whether
defined or not.
 It has same name as the class. It may or may not take
parameters .
 It does not return anything
 The prototype is
 Class name(parameter list);
 Example
Parameterized constructors
 Constructors with arguments are called parameterized
constructors.
Example
Dynamic memory
Operators new and new[]
The new operator offers dynamic storage allocation similar to
the standard library function malloc.
Throws an exception when memory allocation fails.
pointer = new type
pointer = new type [number_of_elements]
int * b;
b = new int [5];
Operators delete and delete[]
delete pointer;
delete [] pointer;
 Example
Destructor
 When an object is no longer needed it can be
destroyed. A class can have a member function called
destructor which is invoked when an object is
destroyed.
Example
Constructors with default
arguments.
 Constructors with dynamic operations
 Example 2
Copy constructor
 The copy constructor is a special type of parameterized
constructor.
 It copies one object to another.
 It can be called when an object is created and equated
to an existing object at the same time.
 Vector v1(5), v2(5);
 v1= v2; // operator = invoked
 vector v3 = v2; // copy constructor is invoked.
 Vector v3(v2)
 vector *ptr = new vector(v1);
Passing Objects as Arguments
 Methods
 Pass by value
 Example
 Pass and return by reference
 Example
 Array of objects
this pointer
 The this pointer points at the object with respect to which
the function was called.
 this pointer is always a constant pointer.
 The compiler converts the class into a structure with only
data members.
class dist
{ int feet,inch;
public:
int getfeet();
int getinch();
}
struct dist
{
int feet, inch;
};
int getfeet(dist * const);
int getinch(dist * const);
int dist::getfeet()
{
Return feet;
}
int getfeet(dist * const this)
{
return this->feet;
}
OPERATOR OVERLOADING
 Operator overloading feature of C++ is one of the
methods of realizing polymorphism.
 Operator overloading helps in
 Extending capability of operators to operate on user
defined data.
 Data conversion
 Operator overloading extends the semantics of an
operator without changing its syntax.
Unit   ii
Unary operator overloading
 Example with member function
 Example with operator overloading
 Operator keyword
 The keyword operator facilitates overloading of the C++
operators.
Operator return values
 idx1 = idx2++;
 Example
 Binary Operator overloading
Arithmetic operators
 Adding two objects of a class
 Direct Addition
 Overloaded + operator
 String Example
Comparison operators
 Example
 Arithmetic assignment operators
Data conversion
 Example
Conversion between objects of
different classes.
 ClassA objecta;
 ClassB objectb;
 objecta = objectb;
 Conversion routine in the source object’s class is
implemented using an operator function
 In objecta= objectb, objectb is the source object of
classB
Conversion between objects of
different classes.
 Example
Conversion routine in destination
object: Constructor function
 Example
Overloading with friend functions
Example
Example 2
Inheritance
 Inheritance is a technique of organizing information
in a hierarchical form.
 Inheritance allows new classes to be built from older
and less specialized class.
 Classes are created by first inheriting all the variables
and behavior defined by some primitive class and then
adding specialized variables and behaviors.
 Inheritance allows code reusability.
Unit   ii
Base class and derived class
Unit   ii
 Example
TYPES OF INHERITANCE
Unit   ii
Unit   ii
Constructors
Zero argument constructor in base and derived class
Example
Parameterized constructors in base and derived classes
Example
Parameterized constructor in derived class
Example
Multilevel Inheritance
 Derivation of a class from another derived class is
called multilevel inheritance.
 Example
 Constructor Example
Multiple Inheritance
 Example
 Example
Hierarchical Inheritance
Example
Multipath Inheritance
 The form of inheritance which derives a new class by
multiple inheritance of base classes, which are derived
earlier form the same base class is known as multipath
inheritance.
 Example
 Problems with multipath inheritance:
 Ambiguity due to duplicate copies of members
inherited from the base class.
 Virtual Base class is used to solve the ambiguity issue in
multipath inheritance.
Hybrid inheritance
 Write a program to implement hybrid inheritance
shown in the diagram. Consider necessary details for
all the classes.

More Related Content

PPTX
constructors in java ppt
PPTX
Class object method constructors in java
PPTX
Constructor in java
PPTX
PPT
Java oops PPT
PPT
C++ classes
PDF
Constructors destructors
PDF
Inheritance
constructors in java ppt
Class object method constructors in java
Constructor in java
Java oops PPT
C++ classes
Constructors destructors
Inheritance

What's hot (20)

PPTX
Classes, objects in JAVA
PDF
Method overloading, recursion, passing and returning objects from method, new...
PDF
ITFT-Classes and object in java
PPTX
Constructor&method
PDF
Constructors and destructors
PPTX
Constructor ppt
PPT
Java Concepts
PPT
Lecture 2
PPS
Introduction to class in java
PPT
Classes & objects new
PDF
Constructors and Destructors
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PDF
Function overloading
PPTX
Lecture 4.2 c++(comlete reference book)
PDF
Lect 1-java object-classes
PPT
Java lec class, objects and constructors
PDF
PPTX
Java(Polymorphism)
PPTX
oop lecture 3
PDF
Classes and objects
Classes, objects in JAVA
Method overloading, recursion, passing and returning objects from method, new...
ITFT-Classes and object in java
Constructor&method
Constructors and destructors
Constructor ppt
Java Concepts
Lecture 2
Introduction to class in java
Classes & objects new
Constructors and Destructors
Oop Constructor Destructors Constructor Overloading lecture 2
Function overloading
Lecture 4.2 c++(comlete reference book)
Lect 1-java object-classes
Java lec class, objects and constructors
Java(Polymorphism)
oop lecture 3
Classes and objects
Ad

Viewers also liked (20)

PPTX
Projects Department Weekly Report - SVC AlJouf-2, Dynamic and Static Rea... d...
PPT
Shagree sintesi primovolotermico
PPT
Presentazione A@gres orientagate 2013
PDF
MM Bagali,,,,,,,,, HR, HRM, HRD, PhD research, India.......High performance ....
PDF
Biz Presentation 2013
PPTX
Technology Hill SMO PowerPoint
PDF
Заявление министерства юстиции
PDF
Solid Waste Management presentation to KSPCB
DOCX
Dhaka South City Corporation: Structure, Finance and Personal Management
PPTX
Las condiciones-del-aprendizaje por Dennis Montufar
ODP
environmental control of poultry farms PLC based project presentation
PDF
Presentation 2 student x
PDF
Progetto Digital Strategy Amadori
PPTX
EXPONENTES DEL CONDUCTISMO
PPT
Web analytics: i cruscotti
PPTX
ERTMS Presentation
PPTX
Need and Importance of ICT Based Library Services in ODL: With Special Refere...
PPT
Raggiungere 500 collegamenti su LinkedIn
PPTX
[마케팅전략] 2012년 게임어플리케이션 마케팅 전략 및 실행안
PDF
Plan de Trabajo Min. Adolescente UPSur
Projects Department Weekly Report - SVC AlJouf-2, Dynamic and Static Rea... d...
Shagree sintesi primovolotermico
Presentazione A@gres orientagate 2013
MM Bagali,,,,,,,,, HR, HRM, HRD, PhD research, India.......High performance ....
Biz Presentation 2013
Technology Hill SMO PowerPoint
Заявление министерства юстиции
Solid Waste Management presentation to KSPCB
Dhaka South City Corporation: Structure, Finance and Personal Management
Las condiciones-del-aprendizaje por Dennis Montufar
environmental control of poultry farms PLC based project presentation
Presentation 2 student x
Progetto Digital Strategy Amadori
EXPONENTES DEL CONDUCTISMO
Web analytics: i cruscotti
ERTMS Presentation
Need and Importance of ICT Based Library Services in ODL: With Special Refere...
Raggiungere 500 collegamenti su LinkedIn
[마케팅전략] 2012년 게임어플리케이션 마케팅 전략 및 실행안
Plan de Trabajo Min. Adolescente UPSur
Ad

Similar to Unit ii (20)

PPTX
C++ & Data Structure - Unit - first.pptx
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PPTX
OBJECT ORIENTED PROGRAMING IN C++
PDF
A COMPLETE FILE FOR C++
PPTX
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
PPTX
c++.pptxwjwjsijsnsksomammaoansnksooskskk
PPTX
Class and object
PPT
Object Oriented Technologies
PDF
Unit3_OOP-converted.pdf
PPTX
Object Oriented Programming Using C++
PPTX
oopusingc.pptx
PDF
Classes-and-Objects-in-C++.pdf
PPTX
PPTX
C++ tutorial assignment - 23MTS5730.pptx
PPTX
C++ programming introduction
PPTX
concept of oops
PDF
Class and object
C++ & Data Structure - Unit - first.pptx
Chapter 2 OOP using C++ (Introduction).pptx
OBJECT ORIENTED PROGRAMING IN C++
A COMPLETE FILE FOR C++
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
c++.pptxwjwjsijsnsksomammaoansnksooskskk
Class and object
Object Oriented Technologies
Unit3_OOP-converted.pdf
Object Oriented Programming Using C++
oopusingc.pptx
Classes-and-Objects-in-C++.pdf
C++ tutorial assignment - 23MTS5730.pptx
C++ programming introduction
concept of oops
Class and object

More from donny101 (9)

PPTX
Unit v
PPTX
Unit iv
PPTX
Unit iii
PPTX
Unit 1
PDF
Unit vos - File systems
PDF
Unit ivos - file systems
PDF
Unit iios process scheduling and synchronization
PDF
Unit iiios Storage Management
PDF
Unit 1os processes and threads
Unit v
Unit iv
Unit iii
Unit 1
Unit vos - File systems
Unit ivos - file systems
Unit iios process scheduling and synchronization
Unit iiios Storage Management
Unit 1os processes and threads

Recently uploaded (20)

PPTX
web development for engineering and engineering
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Digital Logic Computer Design lecture notes
PDF
composite construction of structures.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Welding lecture in detail for understanding
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Well-logging-methods_new................
PPT
Mechanical Engineering MATERIALS Selection
web development for engineering and engineering
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Model Code of Practice - Construction Work - 21102022 .pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Foundation to blockchain - A guide to Blockchain Tech
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Digital Logic Computer Design lecture notes
composite construction of structures.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Arduino robotics embedded978-1-4302-3184-4.pdf
UNIT 4 Total Quality Management .pptx
CYBER-CRIMES AND SECURITY A guide to understanding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
additive manufacturing of ss316l using mig welding
Welding lecture in detail for understanding
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Operating System & Kernel Study Guide-1 - converted.pdf
Well-logging-methods_new................
Mechanical Engineering MATERIALS Selection

Unit ii

  • 2. Structure Definitions struct time { int hour; int min; int second; } Accessing members of structure time t; cout<<t.hour; time * timeptr; Using pointers timeptr = &t; cout<<timeptr->hour; (*timeptr).hour; Example
  • 3. Classes and Objects  A class can be described as a collection of data members and member functions.  Functions are called member functions and define a set of operations that can be performed on the data members of the class.  The association of data and member functions are called encapsulation.
  • 5. Class objects  The process of creating objects(variables) of the class is called class instantiation. Necessary resources are allocated only when the class is instantiated.  The syntax is as follows:  Class className ObjectName…..  Ex: class student s1;
  • 6. Accessing Class members  Class members are accessed using member access operator , dot(.)  Syntax : ObjectName.Datamember;  Example: Student s1;  s1.rollno;  s1.printdetails();
  • 7. Defining member functions  Member functions of the class can be defined in any one of the following ways  Inside the class specification  Outside the class specification
  • 8. Member functions outside the class body Example
  • 9. Access specifiers  Private: The private members of a class have strict access control. Only the member functions of the same class can access these members. It prevents accidental modifications of the data members. class person { private: int age; int getage(); }; Person p1; p1.age = 10; // cannot access private data p1.getage(); // cannot access private function
  • 10. Public members  The members of a class which are visible outside the class are declared in public section. class person { public: int age; int getage(); }; Person p1; p1.age = 10; // can access public datat p1.getage(); // can access public function
  • 11. Protected members  The access control of protected members is similar to private members and has significance to inheritance.  Empty classes;  Class xyz{};  Class Empty {};
  • 12. Constructors and Destructors  Constructors:  The constructor gets called automatically for each object that has just got created.  It appears as member function of each class, whether defined or not.  It has same name as the class. It may or may not take parameters .  It does not return anything  The prototype is  Class name(parameter list);  Example
  • 13. Parameterized constructors  Constructors with arguments are called parameterized constructors. Example
  • 14. Dynamic memory Operators new and new[] The new operator offers dynamic storage allocation similar to the standard library function malloc. Throws an exception when memory allocation fails. pointer = new type pointer = new type [number_of_elements] int * b; b = new int [5]; Operators delete and delete[] delete pointer; delete [] pointer;  Example
  • 15. Destructor  When an object is no longer needed it can be destroyed. A class can have a member function called destructor which is invoked when an object is destroyed. Example
  • 17.  Constructors with dynamic operations  Example 2
  • 19.  The copy constructor is a special type of parameterized constructor.  It copies one object to another.  It can be called when an object is created and equated to an existing object at the same time.  Vector v1(5), v2(5);  v1= v2; // operator = invoked  vector v3 = v2; // copy constructor is invoked.  Vector v3(v2)  vector *ptr = new vector(v1);
  • 20. Passing Objects as Arguments  Methods  Pass by value  Example  Pass and return by reference  Example  Array of objects
  • 21. this pointer  The this pointer points at the object with respect to which the function was called.  this pointer is always a constant pointer.  The compiler converts the class into a structure with only data members. class dist { int feet,inch; public: int getfeet(); int getinch(); } struct dist { int feet, inch; }; int getfeet(dist * const); int getinch(dist * const);
  • 22. int dist::getfeet() { Return feet; } int getfeet(dist * const this) { return this->feet; }
  • 23. OPERATOR OVERLOADING  Operator overloading feature of C++ is one of the methods of realizing polymorphism.  Operator overloading helps in  Extending capability of operators to operate on user defined data.  Data conversion  Operator overloading extends the semantics of an operator without changing its syntax.
  • 25. Unary operator overloading  Example with member function  Example with operator overloading  Operator keyword  The keyword operator facilitates overloading of the C++ operators.
  • 26. Operator return values  idx1 = idx2++;  Example  Binary Operator overloading
  • 27. Arithmetic operators  Adding two objects of a class  Direct Addition  Overloaded + operator  String Example
  • 28. Comparison operators  Example  Arithmetic assignment operators
  • 31. Conversion between objects of different classes.  ClassA objecta;  ClassB objectb;  objecta = objectb;  Conversion routine in the source object’s class is implemented using an operator function  In objecta= objectb, objectb is the source object of classB
  • 32. Conversion between objects of different classes.  Example
  • 33. Conversion routine in destination object: Constructor function  Example
  • 34. Overloading with friend functions Example Example 2
  • 35. Inheritance  Inheritance is a technique of organizing information in a hierarchical form.  Inheritance allows new classes to be built from older and less specialized class.  Classes are created by first inheriting all the variables and behavior defined by some primitive class and then adding specialized variables and behaviors.  Inheritance allows code reusability.
  • 37. Base class and derived class
  • 43. Constructors Zero argument constructor in base and derived class Example Parameterized constructors in base and derived classes Example Parameterized constructor in derived class Example
  • 44. Multilevel Inheritance  Derivation of a class from another derived class is called multilevel inheritance.  Example  Constructor Example
  • 47. Multipath Inheritance  The form of inheritance which derives a new class by multiple inheritance of base classes, which are derived earlier form the same base class is known as multipath inheritance.  Example
  • 48.  Problems with multipath inheritance:  Ambiguity due to duplicate copies of members inherited from the base class.  Virtual Base class is used to solve the ambiguity issue in multipath inheritance.
  • 50.  Write a program to implement hybrid inheritance shown in the diagram. Consider necessary details for all the classes.