Oop C++
Oop C++
Object-Oriented Programming
Introduction to Classes
Object-Oriented Programming
2
General Introduction
Class Definition
Class Examples
Objects
Constructors
Destructors
Object-Oriented Programming
6
Class Definition
Class can be defined as a blueprint of the object. It is basically a collection of objects
which act as building blocks. This is a logical method to organize data and functions in the
same structure.
Object-Oriented Programming
7
Create a Class
To create a class, use the class keyword:
A class contains data members (variables) and member functions. These member functions
are used to manipulate the data members inside the class.
Object-Oriented Programming
8
Permission Labels
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.
You will learn more about Inheritance later.
Note:
If we declare members of a class before including any permission label, the members are
considered private, since it is the default permission that the members of a class declared
with the class keyword acquire.
Object-Oriented Programming
9
Data Members
Can be of any type, built-in or user-defined
non-static data member
Each class object has its own copy
Declaration of an Object
Object-Oriented Programming
11
Accessing a Class:
Accessing Public Data Members
Following is an example to show you how to initialize and use the public data members
using the dot (.) operator and the respective object of class.
Object-Oriented Programming
12
Accessing a Class:
Accessing Private Data Members
Protected data members, can be accessed directly using dot (.) operator inside
the subclass of the current class, for non-subclass we will have to follow the steps same as
to access private data member.
Object-Oriented Programming
14
Example 1:
Object-Oriented Programming
15
Example 2:
Object-Oriented Programming
16
Example 3:
Object-Oriented Programming
17
Example 4:
We have 4 members:
• 2 private of type int.
• 2 public functions that we include only
their prototype
Object-Oriented Programming
18
Demo Full Code
Here is the complete example of class “Rectangle”:
Object-Oriented Programming
19
Scope Operator( :: )
It is used for following purposes.
1) To access a global variable when there is a local variable with same name:
Object-Oriented Programming
20
Scope Operator( :: )
It is used for following purposes.
3) Refer to a class inside another class: If a class exists inside another class we can
use the nesting class to refer the nested class using the scope resolution operator
Object-Oriented Programming
22
Scope Operator( :: )
Example:
Object-Oriented Programming
23
Constructors
Definition:
Constructor: is a special member function which enables an object to initialize itself when
it is created. Constructors have the same name as the class and may be defined inside or
outside the class definition.
Default Constructors
Parameterized constructors
Object-Oriented Programming
27
Default Constructor
Default constructor is the constructor which doesn’t take any argument. It has no
parameters. It is also called a zero-argument constructor.
Example:
Object-Oriented Programming
28
Default Constructor
Example:
Note: Even if we do not define any constructor explicitly, the compiler will automatically
provide a default constructor implicitly.
Object-Oriented Programming
29
Parameterized Constructor
It is possible to pass arguments to constructors. Typically, these arguments help
initialize an object when it is created. To create a parameterized constructor, simply add
parameters to it the way you would to any other function. When you define the constructor’s
body, use the parameters to initialize the object.
Example:
Object-Oriented Programming
30
Parameterized Constructor
Example:
Object-Oriented Programming
31
Parameterized Constructor
Example: Constructor definition outside the class.
Object-Oriented Programming
32
Destructors
Definition