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.
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.
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.
Base class and derived class
 Example
TYPES OF INHERITANCE
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.

Unit ii

  • 2.
    Structure Definitions struct time { inthour; 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  Theprocess 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 outsidethe 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  Themembers 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  Theaccess 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  Constructorswith arguments are called parameterized constructors. Example
  • 14.
    Dynamic memory Operators newand 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 anobject 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
  • 16.
  • 17.
     Constructors withdynamic operations  Example 2
  • 18.
  • 19.
     The copyconstructor 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 asArguments  Methods  Pass by value  Example  Pass and return by reference  Example  Array of objects
  • 21.
    this pointer  Thethis 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; } intgetfeet(dist * const this) { return this->feet; }
  • 23.
    OPERATOR OVERLOADING  Operatoroverloading 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  Addingtwo objects of a class  Direct Addition  Overloaded + operator  String Example
  • 28.
    Comparison operators  Example Arithmetic assignment operators
  • 29.
  • 30.
  • 31.
    Conversion between objectsof 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 objectsof different classes.  Example
  • 33.
    Conversion routine indestination object: Constructor function  Example
  • 34.
    Overloading with friendfunctions Example Example 2
  • 35.
    Inheritance  Inheritance isa 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 andderived class
  • 39.
  • 40.
  • 43.
    Constructors Zero argument constructorin base and derived class Example Parameterized constructors in base and derived classes Example Parameterized constructor in derived class Example
  • 44.
    Multilevel Inheritance  Derivationof a class from another derived class is called multilevel inheritance.  Example  Constructor Example
  • 45.
  • 46.
  • 47.
    Multipath Inheritance  Theform 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 withmultipath 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.
  • 49.
  • 50.
     Write aprogram to implement hybrid inheritance shown in the diagram. Consider necessary details for all the classes.