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.
4.
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
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
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
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);
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.
24.
25. Unary operator overloading
Example with member function
Example with operator overloading
Operator keyword
The keyword operator facilitates overloading of the C++
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
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.
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
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.