0% found this document useful (0 votes)
5 views35 pages

Oop C++

The document provides an introduction to Object-Oriented Programming (OOP), emphasizing its structure around objects rather than functions. It covers key concepts such as classes, objects, constructors, and destructors, explaining how to define and manipulate them in C++. Additionally, it discusses access modifiers and the role of data members in class design.

Uploaded by

zanouba.d2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views35 pages

Oop C++

The document provides an introduction to Object-Oriented Programming (OOP), emphasizing its structure around objects rather than functions. It covers key concepts such as classes, objects, constructors, and destructors, explaining how to define and manipulate them in C++. Additionally, it discusses access modifiers and the role of data members in class design.

Uploaded by

zanouba.d2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

1

Object-Oriented Programming

Introduction to Classes
Object-Oriented Programming
2
General Introduction

Object-oriented programming is an approach or


a programming pattern where the programs are structured around
objects rather than functions and logic. It makes the data partitioned
into two memory areas, i.e., data and functions, and helps make the
code flexible and modular.
Object-oriented programming mainly focuses on objects that are
required to be manipulated. In OOPs, it can represent data as objects
that have attributes and functions.
Object-Oriented Programming
3
General Introduction
Object-oriented programming (OOP) is a computer programming model that organizes
software design around data, or objects
Object-Oriented Programming
4
 A calendar program might want to store information
about dates, but C++ does not have a Date type.

 A student registration system needs to store info


about students, but C++ has no Student type.

 A bank app might want to store information about


users' accounts, but C++ has no BankAccount type.

 However, C++ does provide a feature for us to add


new data types to the language: classes.
 Writing a class defines a new data type.
Object-Oriented Programming
5
Introduction to Classes

 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

 static data member


 Acts as a global variable

 One copy per class type.


Object-Oriented Programming
10
Objects
An Object is an instance of a Class. When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is allocated. Data can be accessed
by its functions.

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

To access, use and initialize the private


data member you need to create getter and
setter functions, to get and set the value of
the data member.

The setter function will set the value


passed as argument to the private data
member, and the getter function will
return the value of the private data
member to be used. Both getter and setter
function must be defined public.
Object-Oriented Programming
13
Accessing a Class:
Accessing Protected 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:

• Declares class “Rectangle” and an object


called “rect” of this class (type).

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.

2) To define a function outside a class.


Object-Oriented Programming
21
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.

 Important characteristics of constructor are


 It should be defined in public section only.
 It has same name as the name of the class.
 It is invoked automatically when an object of its associated class is created.
 Constructor does not return any value.
Object-Oriented Programming
24
Constructors
Syntax Declaration
class_name
{
public:
class_name( parameter list)
{
function body //constructor function
}
};
Object-Oriented Programming
25
Constructors
Example:
class MyClass
{
public:
MyClass ()
{
cout << "Hello World!";
}
}
int main() {
MyClass myObj; // Create an object of MyClass (this will call the constructor)
return 0;
}
Object-Oriented Programming
26
Constructors
Types Of Constructors:

The basic types of constructor available in C++ are :

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

 It is a special function just opposite constructor. Unlike constructors that are


used for initializing an object, destructors destroy (or delete) the object.
 The important characteristics of destructor are :
1. Destructor function also has the same name as the class but preceded by a
tilde (~).
2. When an object is out of scope destructor function is called automatically.
3. The destructor has no arguments.
4. The destructor does not return any value.
5. There cannot be more than one destructor in a class.
Object-Oriented Programming
33
Destructors
Syntax Declaration
class_name
{
Public:
class_name( parameter list)
{

function body // constructor function


}
~ class_name()
{

function body // destructor function


}
};
Object-Oriented Programming
34
Destructors
Example
Object-Oriented Programming
35
Destructors
Example

You might also like