Classes and Objects-2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Classes and Objects

September 26, 2022

Classes and Objects September 26, 2022 1 / 21


Outline
1 Introduction
2 Classes in C++
3 Objects in C++
4 Access Specifiers and their scope
5 Defining Member Functions
6 Characteristics of Member Function
7 Outside member function as inline
8 Rules for inline functions
9 static member variables
10 static member functions
11 static objects
12 object as function arguments
13 Friend Function
Classes and Objects September 26, 2022 2 / 21
Introduction

Introduction

In C, Structure is a user defined type which is a combination of same


or different data types. Only variables are declared inside a structure.
The initialization of member variables inside the structure is not
permitted
Objects can directly access the data members of the structures.
Functions are not permitted as members in structure.
Outside functions are also able to access the data members of the
structure through the object
Thus, there is no security to data members declared inside the
structure in C

Classes and Objects September 26, 2022 3 / 21


Classes in C++

Classes in C++

The fundamental building block that leads to object-oriented


programming in C++ is class.
A C++ class is like a blueprint for an object.
A class is a user-defined data type that contains its data members
and member functions.
Each variable of a class is called as member variable or data member
Functions are called as member functions or methods
Data members and member functions of a class are together called
‘class members’.

Classes and Objects September 26, 2022 4 / 21


Classes in C++

Creating Classes in C++

A class in C++ is defined using the keyword class followed by class name.
The body of the class is defined inside curly braces and terminated using a
semicolon.

Classes and Objects September 26, 2022 5 / 21


Classes in C++

Class Example

class Rectangle{
public:
int length, breadth;

int area(){
return length * breadth;
}
};

Classes and Objects September 26, 2022 6 / 21


Objects in C++

Objects in C++

An instance of a class is called an object.


When a class is defined, only the specification for the object is
defined; no memory or storage is allocated.
To use the data and access functions defined in the class, you need to
create objects.
Syntax:
ClassName ObjectName;
Accessing data members and member functions:
We can access data members and member functions using dot (‘.’)
operator.
Syntax:
ObjectName.VariableName; //Accessing data member
ObjectName.FunctionName(); //Accessing member function

Classes and Objects September 26, 2022 7 / 21


Access Specifiers and their scope

Access Specifiers and their scope


C++ access specifiers are used for determining the accessibility or visibility
level of class members. There are three access specifiers:
1 public

2 private

3 protected

By default the class members are private

Classes and Objects September 26, 2022 8 / 21


Defining Member Functions

Defining Member Functions

A member function can be defined in the following ways:


Member Function Inside the class
Member Function Outside the class
returntype classname::functionname()
Private Member Function A function declared inside the class’s
private section is known as ”private member function”. A private
member function is accessible through the only public member
function.

Classes and Objects September 26, 2022 9 / 21


Characteristics of Member Function

Characteristics of Member Function

1 The Difference between member and normal function is that normal


function can be invoked freely where as the member function can be
accessed only by the object of the class.
2 The same function can be used in any number of classes because
scope of function is limited to their classes.
3 he private data (or) private function can be accessed by public
Member Function
4 The member function can be accessed one another without using any
object (or) dot Operator.

Classes and Objects September 26, 2022 10 / 21


Outside member function as inline

Outside member function as inline

It is good practice to declare function prototype inside class and


definition outside the class
The inline mechanism reduces the overhead relating to access the
member function
t provides better efficiency quick execution of function.
Inline Function similar to Macros
Call to inline function in the program places the function code in the
caller program.• This is known as inline expansion.
By default all member functions defined inside the class are inline.
The member function defined outside the class can be made inline by
prefixing keyword inline.
Syntax:
inline returntype classname::funname()

Classes and Objects September 26, 2022 11 / 21


Rules for inline functions

Rules for inline functions

Inline function should be used rarely.


Inline function can be used when the member function contains few
statements
If function takes more time to execute, then it must be declared as
inline.

Classes and Objects September 26, 2022 12 / 21


Rules for inline functions

Assignment

1) Write a program to calculate simple interest. Hide the data elements of


the class using private keyword.
2) Write a program to declare class with private, public and protected
sections. Declare object and access data elements of these different
sections.

Classes and Objects September 26, 2022 13 / 21


Rules for inline functions

Classes,Objects and Memory

Objects are the identifiers declared for class data type.


Each object has its own copy of public and private data members.
All the objects of a class use the same member functions.
Only one copy of member function is created and stored in the
memory whereas each object has its own set of data members.

Classes and Objects September 26, 2022 14 / 21


Rules for inline functions

Data members and member functions in memory

Classes and Objects September 26, 2022 15 / 21


static member variables

Static member variables


if a member variable is declared as static only one copy of the
member is created for the whole class.
The static is a keyword that is used to preserve the value of a variable
When a variable is declared as static it is initialized to zero
The static member variable is only visible within the class but its
lifetime is till the program ends.
static member variable is defined outside the class
declaration.Reasons are as follows
1 The static data members are associated with the class and not with
any object.
2 The static data member variable must be initialized otherwise the
linker will generate an error.
3 The memory for static data is allocated only once
4 Only one copy of static member variable is created for the whole class
for any number of objects. All the objects have common static data
member.
Classes and Objects September 26, 2022 16 / 21
static member functions

Static Member Functions

Points to be followed while declaring static function:


Just one copy of a static member is created in the memory for entire
class. All objects of the class share the same copy of static member.
static member function can access only static data members or
functions
static member function can be invoked using class name
It is also possible to invoke static member functions using objects.
When one of the objects changes the value of data member variables,
the effect is visible to all the object of the class
static Private Member Function A static member function can also be
declared in private section. The private static function must be invoked
using static public function

Classes and Objects September 26, 2022 17 / 21


static objects

static object

The static keyword can be used to initialize all class data member
variables to zero.
Declaring object itself as static can do this. Thus all its associated
members get initialized to zero.

Classes and Objects September 26, 2022 18 / 21


object as function arguments

object as function arguments

Similar to variables, object can be passed to functions.


The following are the three methods to pass argument to a
function:
Pass by Value: A copy of object (actual object) is sent to function
and assigned to the object of callee function (formal object).
Pass-by-reference: Address of object is implicitly sent to function.
Pass by Reference: Address of the object is explicitly sent to
function.

Classes and Objects September 26, 2022 19 / 21


Friend Function

Friend Function
The private members of the class are accessed only from member
functions of that class.
C++ allows a mechanism, in which a non-member function has access
permission to the private members of the class.
This can be done by declaring a non-member function friend to the class
whose private data is to be accessed.
class ac
{
private:
char name [15];
int acno;
float bal;
public:
void read();
friend void showbal();
}
Classes and Objects September 26, 2022 20 / 21
Friend Function

Friend Function Properties


The function can be declared as friend function in one or more classes.
The keyword friend or scope access operator must not precede the
definition of the friend function.
There is no scope restriction for the friend function; hence, they can
be called directly without using objects.
The declaration of friend function is done inside the class in private or
public part
a function can be declared as friend function in any number of classes.
These functions use objects as arguments.
Unlike member functions of class, the friend cannot access the
member directly. On the other hand it uses object and dot operator
to access the private and public member variables of the class
By default, friendship is not shared (mutual). For example, if class X
is declared as friend of Y, this does not meant that Y has privileges to
access private members of class X
Use of friend functions is rare, since it violates the rule of
Classes and Objects September 26, 2022 21 / 21

You might also like