SlideShare a Scribd company logo
1 of 71
CLASSES AND
OBJECTS
OBJECTIVES OF THIS SESSION
 Structures in C and its limitations
 Specifying a Class
 Creating Objects
 Accessing Class Members
 Defining Member Functions
 Making an outside Function Inline
 Nesting of Member Functions
 Private Member Functions
INTRODUCTION
 Classes is an extension of the idea of structure
used in C.
 It is a new way of creating and implementing a
user-defined data type.
STRUCTURES IN C
 A structure is a convenient tool for handling a group
of logically related data items.
 It is a user defined data type with a template.
 Once the structure type has been defined, we can
create variables of that type using declarations, that
are similar to the built-in type declarations.
STRUCTURES IN C
struct student
{
char name[20];
int roll_number;
float total_marks;
};
The keyword struct declares student as a new
data type that can hold three fields of different
data types.
struct student A; // C declaration
continue…
Structure members or elements
Structure name or structure tag
LIMITATIONS OF STRUCTURES IN C
 The standard C does not allow the struct data type to be
treated like built-in types.
 They do not permit data hiding.
 Structure members can be directly accessed by the
structure variables by any function anywhere in their
scope.
STRUCTURES AND CLASSES IN C++
 C++ supports all the features of structures as defined in
C.
 In C++, a structure can have both variables and functions
as members.
 It can declare some of its members as ‘private’.
 In C++, the structure names are stand-alone and can be
used like any other type names.
student A; // C++ declaration
STRUCTURES AND CLASSES IN C++
 By default the members of a class are private, while, by
default, the members of a structure are public.
continue…
CLASS
 A class is a way to bind the data and its associated
functions together.
 It allows the data( and functions ) to be hidden, if
necessary, from external use.
 A CLASS specification has two parts:
 Class Declaration
 Class Function Definitions
Describes the type and scope of its
members
Describes how the class functions are implemented
CLASS DECLARATION
class class_name
{
private :
variable declarations;
function declarations;
public :
variable declarations;
function declarations;
};
The class declaration is similar to a struct
declaration.
CLASS DECLARATION
 The body of a class is
enclosed within braces
and terminated by a
semicolon.
 The class body contains
the declaration of
variables and functions.
 These functions and
variables collectively
called class members.
continue…
class class_name
{
private :
variable declarations;
function declarations;
public :
variable declarations;
function declarations;
};
CLASS DECLARATION
 Members grouped into
two sections :
 Private - visibility labels
 Public
 The keyword are followed
by colon.
continue…
class class_name
{
private :
variable declarations;
function declarations;
public :
variable declarations;
function declarations;
};
CLASS DECLARATION
 The class members that
have been declared as
private can be accessed
only from within the class.
 Public members can be
accessed from outside the
class also.
 Keyword private is
optional. By default, the
members of a class are
private.
continue…
class class_name
{
private :
variable declarations;
function declarations;
public :
variable declarations;
function declarations;
};
CLASS DECLARATION
class class_name
{
private :
variable declarations;
function declarations;
public :
variable declarations;
function declarations;
};
 The variables declared inside
the class are known as data
members.
 and the functions are known
as member functions.
 Only the member functions
can have access to the private
data members and private
functions.
continue…
CLASS DECLARATION
class class_name
{
private :
variable declarations;
function declarations;
public :
variable declarations;
function declarations;
};
 The public members (both
functions and data) can be
accessed from outside the
class.
 The binding of data and
functions together into a single
class-type variable is referred
to as encapsulation.
continue…
CLASS DECLARATION continue…
Data
Functions
Private area
Data
Functions
Public area
No entry to
private area
X
Entry allowed to
public area
Data hiding in CLASS
CLASS EXAMPLE
class item
{
int number; // variable declaration
float cost; // private by default
public :
void getdata( int a, float b); // function declaration
void putdata( void ); // using prototype
};
CLASS EXAMPLE
 Give meaningful names to
classes.
 Names become the new
type identifier that can be
used to declare instances
of that class type.
 The class item contains
two data members and
two member functions.
continue…
class item
{
int number;
floatcost;
public :
void getdata(int a, float b);
void putdata(void);
};
CLASS EXAMPLE
 The data members are
private by default
 While both the functions
are public by declaration.
 The functions are
declared but not defined.
 Actual function definition
will appear later in the
program.
continue…
class item
{
int number;
floatcost;
public :
void getdata(int a, float b);
void putdata(void);
};
CLASS EXAMPLE continue…
class item
{
int number;
floatcost;
public :
void getdata(int a, float b);
void putdata(void);
};
Class : ITEM
DATA
number
cost
………
FUNCTIONS
getdata( )
putdata( )
………
Representation of a class
CREATING OBJECTS
Once a class has been declared, we can create variables
of that type by using the class name.
item x ; // create a variable x of type item.
In C++, the class variables are known as objects.
item x, y, z ; // declare more than one objects in one statement
CREATING OBJECTS
 The declaration of an object is similar to that of any
basic type.
 The necessary memory space is allocated to an object
at this stage.
 Class specification, like a structure, provides only a
template and does not create any memory space for the
objects.
continue…
CREATING OBJECTS
 Object can also be created when a class is defined by
placing their names immediately after the closing brace.
class item
{
……
……
……
} x, y, z ;
continue…
ACCESSING CLASS MEMBERS
 The private data of a class can be accessed only
through the member functions of that class.
object-name . function-name ( actual-
arguments);
In our example, although x is an object of the type
item to which number belongs, the number can
be accessed only through a member function
and not by the object directly.
DEFINING MEMBER FUNCTIONS
 Member functions can be defined in two places:
 Outside the class definition.
 Inside the class definition.
DEFINING MEMBER FUNCTIONS
 Outside the Class Definition
 Member functions that are declared inside a class have to be
defined separately outside the class.
 Their definitions are very much like the normal functions.
 They should have a function header and a function body.
 An important difference between a member function and a
normal function is that a member function incorporates a
membership “identity label” in the header.
continue…
This label tells the compiler which
class the function belongs to.
DEFINING MEMBER FUNCTIONS
 Outside the Class Definition
return-type class-name : : function-name (argument declaration)
{
Function body
}
 The membership label class-name : : tells the
compiler that the function function-name
belongs to the class class-name.
 The scope of the function is restricted to the
class-name specified in the header line.
continue…
DEFINING MEMBER FUNCTIONS
 Inside the Class Definition
 Replace the function declaration with the
definition of the function inside the class.
 When a function is defined inside a class,
it is treated as an inline function.
 All the restrictions and limitations that
apply to an inline function are also
applicable to the functions defined inside a
class.
continue…
ADVANTAGE OF INLINE FUNCTIONS
 It speeds up your program by avoiding function calling overhead.
 It save overhead of variables push/pop on the stack, when function
calling happens.
 It save overhead of return call from a function.
 It increases locality of reference by utilizing instruction cache.
 By marking it as inline, you can put a function definition in a
header file (i.e. it can be included in multiple compilation unit,
without the linker complaining)
DISADVANTAGE OF INLINE FUNCTION
 It increases the executable size due to code expansion.
 C++ inlining is resolved at compile time. Which means if you
change the code of the inlined function, you would need to
recompile all the code using it to make sure it will be updated.
 When used in a header, it makes your header file larger with
information which users don’t care.
 As mentioned above it increases the executable size, which may
cause thrashing in memory. More number of page fault bringing
down your program performance.
 Sometimes not useful for example in embedded system where
large executable size is not preferred at all due to memory
constraints.
USABILITY OF INLINE FUNCTION
 Use inline function when performance is needed.
 Use inline function over macros.
 Prefer to use inline keyword outside the class with the
function definition to hide implementation details.
MAKING AN OUTSIDE AND INSIDE
FUNCTIONS
MAKING AN OUTSIDE AND INSIDE
FUNCTIONS
MAKING AN OUTSIDE FUNCTIONS
INLINE
 The member functions defined outside a class
can be made inline by using the qualifier inline
in the header line of function definition.
class item
{
……
……
public :
void getdata (int a, float b);
};
inline void item : : getdata (int a, float b)
{
number = a ;
cost = b ;
}
OUTPUT
 Enter first value: 45
 Enter second value: 15
 Addition of two numbers: 60
 Difference of two numbers: 30
 Product of two numbers: 675
 Division of two numbers: 3
NESTING OF MEMBER FUNCTIONS
 The member function of a class can be called only by
an object of that class using a dot operator.
 But a member function can be called by using its name
inside another member function of the same class.
 This is known as nesting of member functions.
PRIVATE MEMBER FUNCTIONS
 Private member functions can be created for making
them to be hidden.
 A private member function can only be called by
another function that is a member of its class.
 Even an object cannot invoke a private function using
the dot operator.
PRIVATE MEMBER FUNCTIONS
class product
{
int code ;
float stock ;
void read ( void ) ;
public :
void update( void ) ;
void display( void ) ;
};
If p1 is an object, then
p1.read ( ) is illegal.
However, the function
read( ) can be called by
any of the public
functions of this class.
void product : : update (
void)
{
read ( ) ;
};
continue…
ARRAYS WITHIN A CLASS
The arrays can be used
as member variables in a
class.
const int size = 10;
class matrix
{
int mat [ size ] ;
public:
void getval ( ) ;
void putval ( ) ;
};
MEMORY ALLOCATION FOR OBJECTS
 The member functions are created and placed
in the memory space only once when they are
defined.
 Since all the objects belongs to that class use
the same member functions, no separate
space is allocated for member functions when
the objects are created.
 Only space for member variables is allocated
separately for each object.
 Separate memory locations for the objects are
essential, because the member variables hold
different data values for different objects.
STATIC DATA MEMBERS
 A data member of a class can be qualified as
static.
 Characteristics of static member variables:
 It is initialized to zero when the first object of its class
is created. No other initialization is permitted.
 Only one copy of that member is created for the
entire class and is shared by all the objects of that
class, no matter how many objects are created.
 It is visible only within the class, but its lifetime is the
entire program.
 Static variables are normally used to maintain
values common to the entire class.
STATIC DATA MEMBERS
 The type and scope of each static member variable
must be defined outside the class definition.
 This is because the static data members are stored
separately rather than as a part of an object.
 Since they are associated with class itself rather than
with any class object, they are also known as class
variables.
continue…
STATIC DATA MEMBERS
 Static variables are like non-inline member functions as
they are declared in a class declaration and defined in
the source file.
 While defining a static variable, some initial value can
also be assigned to the variable.
 type class-name : : static-variable = initial value;
continue…
STATIC MEMBER FUNCTIONS
 Like static member variable, we can also have static
member functions.
 Properties of member functions:
 A static function can have access to only other static
members ( functions or variables ).
 A static member function can be called using the class name
( instead of its objects ) as:
 class-name : : function-name;
ARRAYS OF OBJECTS
 Arrays of variables that are of type class are called
arrays of objects.
class employee
{
char name [30];
float age;
public:
void getdata (void);
void putdata (void);
};
employee manager [5];
employee worker [25];
• The array manager contains five
objects, viz manager[0],
manager[1], manager[2],
manager[3] & manager[4].
• Array of objects behave like any
other array.
• manager [i]. putdata( ); to execute
the putdata( ) member function of
the ith element of the array
manager.
OBJECTS AS FUNCTION ARGUMENTS
 An object can be used as a function argument like any
other data type.
 Two ways:
 A copy of the entire object is passed to the function. ( Pass-
by-Value)
 Only the address of the object is transferred to the function.
(Pass-by-Reference)
 The pass-by-reference method is more efficient since it
requires to pass only the address of the object and not
the entire object.
OBJECTS AS FUNCTION ARGUMENTS
 An object can also be passed as an argument to a non-
member function.
 Such functions can have access to the public member
functions only through the objects passed as arguments
to it.
 These functions cannot have access to the private data
members.
continue…
FRIENDLY FUNCTIONS
 The private members can not be accessed from outside
the class.
 A non-member function can not have an access to the
private data of a class.
 However ……. ?
FRIENDLY FUNCTIONS
 C++ allows a common function to be made friendly with
more than one classes, thereby allowing the function to
have access to the private data of these classes.
 Such a function need not be a member of these
classes.
 To make an outside function friendly to a class, we have
to simply declare this function as a friend of the class.
continue…
FRIENDLY FUNCTIONS
 The function
declaration should be
preceded by the
keyword friend.
 The function is
defined elsewhere in
the program like a
normal C++ function.
 The function definition
does not use either
the keyword friend or
the scope operator : :.
class employee
{
---
---
public :
---
---
friend void it_cal
(void);
}
continue…
FRIENDLY FUNCTIONS
 The functions that are declared with the keyword
friend are known as friend function.
 A function can be declared as a friend in any
number of classes.
 A friend function, although not a member function,
has full access right to the private members of the
class.
continue…
FRIENDLY FUNCTIONS
Special Characteristics:
 It is not in the scope of the class to which it has
been declared as friend.
 Since it is not in the scope of the class, it cannot
be called using the object of the class.
 It can be invoked like a normal function without
the help of any object.
continue…
FRIENDLY FUNCTIONS
Special Characteristics:
 Unlike member functions, it cannot access the
member names directly and has to use an object
name and dot membership operator with each
member name.
 It can be declared either in the public or private
part of a class without affecting its meaning.
 Usually, it has objects as arguments.
continue…
FRIENDLY FUNCTIONS
Member function of one class can be friend functions
of another class.
In such cases, they are defined using the scope
resolution operator as:
continue…
FRIENDLY FUNCTIONS
class X
{
…
…
int fun1 ( );
…
};
class Y
{
…
…
friend int X : : fun1 (
);
…
};
continue…
FRIENDLY FUNCTIONS
We can also declare all
the member functions of
one class as the friend
functions of another
class.
In such cases, the class
is called a friend class.
class Z
{
…
…
friend class X ;
…
};
continue…
RETURNING OBJECTS
Like a function can receive objects as arguments,
it can also return objects.
CONST MEMBER FUNCTIONS
If a member function does not alter any data in the class,
then it is called a const member function.
void mul (int, int) const ;
void get_balance( ) const ;
The qualifier const is appended to the function prototypes (
in both declaration and definition). The compiler will
generate an error message if such functions try to alter the
data values.
POINTER TO MEMBERS
 It is possible to take the address of a member of a class
and assign it to a pointer.
 The address of a member can be obtained by applying
the operator & to a fully qualified class member name.
 A class member pointer can be declared using the
operator : : * with the class name.
POINTER TO MEMBERS
We can define a pointer to the
member m as follows:
int A : : * pm = &A : : m;
A : : * “pointer-to-member
of A class”.
&A : : m means “address of
the m member of A class”.
class A
{
private :
int m ;
public :
void show( ) ;
} ;
continue…
POINTER TO MEMBERS
The dereferencing operator
.* is used when the object
itself is used with the
member pointer.
The dereferencing operator
->* is used to access a
member when we use
pointers to both the object
and the member.
class A
{
int m ;
public :
void show( ) ;
} ;
A a ;
int A : : * pm = & A : : m ;
A * pa = & a ;
continue…
POINTER TO MEMBERS
The dereferencing operator
.* is used when the object
itself is used with the
member pointer.
The dereferencing operator
->* is used to access a
member when we use
pointers to both the object
and the member.
class A
{
int m ;
public :
void show( ) ;
} ;
A a ;
int A : : * pm = & A : : m ;
A * pa = & a ;
continue…
To refer the member m
a .* pm
To refer the member m
pa -> * pm
POINTER TO MEMBERS
We can also design pointers to member functions which, then,
can be invoked using the dereferencing operators in the main.
(object-name . * pointer-to-member function) ( )
(pointer-to-object -> * pointer-to-member
function) ( )
continue…
The precedence of ( ) is higher than that of . *
and -> * , so the parentheses are necessary.
LOCAL CLASSES
Classes can be defined and used inside a function or a block.
Such classes are called local classes.
Local classes can be used global variables and static
variables but can not use automatic variables. The global
variables should be used with the scope operator ( : : ).
They cannot have static data members and member
functions must be defined inside the local classes.
THANK YOU

More Related Content

What's hot

java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++RAJ KUMAR
 
Classes And Methods
Classes And MethodsClasses And Methods
Classes And Methodsadil raja
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Lecture12 software design class diagram
Lecture12 software design class diagramLecture12 software design class diagram
Lecture12 software design class diagramShahid Riaz
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Uzair Salman
 
Uml Presentation
Uml PresentationUml Presentation
Uml Presentationmewaseem
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Uml class diagram and packages ppt for dot net
Uml class diagram and packages ppt for dot netUml class diagram and packages ppt for dot net
Uml class diagram and packages ppt for dot netmekhap
 
2. oop with c++ get 410 day 2
2. oop with c++ get 410   day 22. oop with c++ get 410   day 2
2. oop with c++ get 410 day 2Mukul kumar Neal
 
Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Rakibul Hasan Pranto
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 

What's hot (20)

Interfaces & Packages V2
Interfaces & Packages V2Interfaces & Packages V2
Interfaces & Packages V2
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Unit 3
Unit 3Unit 3
Unit 3
 
Ppt chapter02
Ppt chapter02Ppt chapter02
Ppt chapter02
 
class diagram
class diagramclass diagram
class diagram
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Classes And Methods
Classes And MethodsClasses And Methods
Classes And Methods
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Lecture12 software design class diagram
Lecture12 software design class diagramLecture12 software design class diagram
Lecture12 software design class diagram
 
Interfaces in java
Interfaces in javaInterfaces in java
Interfaces in java
 
Ppt chapter08
Ppt chapter08Ppt chapter08
Ppt chapter08
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
Uml Presentation
Uml PresentationUml Presentation
Uml Presentation
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Uml class diagram and packages ppt for dot net
Uml class diagram and packages ppt for dot netUml class diagram and packages ppt for dot net
Uml class diagram and packages ppt for dot net
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Ppt chapter07
Ppt chapter07Ppt chapter07
Ppt chapter07
 
2. oop with c++ get 410 day 2
2. oop with c++ get 410   day 22. oop with c++ get 410   day 2
2. oop with c++ get 410 day 2
 
Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)Islamic University Previous Year Question Solution 2018 (ADBMS)
Islamic University Previous Year Question Solution 2018 (ADBMS)
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 

Similar to Class objects oopm

Similar to Class objects oopm (20)

C++ Notes
C++ NotesC++ Notes
C++ Notes
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Class and object
Class and objectClass and object
Class and object
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Class and object
Class and objectClass and object
Class and object
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
My c++
My c++My c++
My c++
 
Object & classes
Object & classes Object & classes
Object & classes
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Class properties
Class propertiesClass properties
Class properties
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Class and object in C++ By Pawan Thakur
Class and object in C++ By Pawan ThakurClass and object in C++ By Pawan Thakur
Class and object in C++ By Pawan Thakur
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 

More from Shweta Shah

CG_Unit1_SShah.pptx
CG_Unit1_SShah.pptxCG_Unit1_SShah.pptx
CG_Unit1_SShah.pptxShweta Shah
 
Windowing clipping
Windowing   clippingWindowing   clipping
Windowing clippingShweta Shah
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONShweta Shah
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++Shweta Shah
 
Virtual function and abstract class
Virtual function and abstract classVirtual function and abstract class
Virtual function and abstract classShweta Shah
 
Introduction to computer graphics and multimedia
Introduction to computer graphics and multimediaIntroduction to computer graphics and multimedia
Introduction to computer graphics and multimediaShweta Shah
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating SystemsShweta Shah
 

More from Shweta Shah (7)

CG_Unit1_SShah.pptx
CG_Unit1_SShah.pptxCG_Unit1_SShah.pptx
CG_Unit1_SShah.pptx
 
Windowing clipping
Windowing   clippingWindowing   clipping
Windowing clipping
 
ConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTIONConsTRUCTION AND DESTRUCTION
ConsTRUCTION AND DESTRUCTION
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Virtual function and abstract class
Virtual function and abstract classVirtual function and abstract class
Virtual function and abstract class
 
Introduction to computer graphics and multimedia
Introduction to computer graphics and multimediaIntroduction to computer graphics and multimedia
Introduction to computer graphics and multimedia
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating Systems
 

Recently uploaded

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 

Recently uploaded (20)

Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 

Class objects oopm

  • 2. OBJECTIVES OF THIS SESSION  Structures in C and its limitations  Specifying a Class  Creating Objects  Accessing Class Members  Defining Member Functions  Making an outside Function Inline  Nesting of Member Functions  Private Member Functions
  • 3. INTRODUCTION  Classes is an extension of the idea of structure used in C.  It is a new way of creating and implementing a user-defined data type.
  • 4. STRUCTURES IN C  A structure is a convenient tool for handling a group of logically related data items.  It is a user defined data type with a template.  Once the structure type has been defined, we can create variables of that type using declarations, that are similar to the built-in type declarations.
  • 5. STRUCTURES IN C struct student { char name[20]; int roll_number; float total_marks; }; The keyword struct declares student as a new data type that can hold three fields of different data types. struct student A; // C declaration continue… Structure members or elements Structure name or structure tag
  • 6. LIMITATIONS OF STRUCTURES IN C  The standard C does not allow the struct data type to be treated like built-in types.  They do not permit data hiding.  Structure members can be directly accessed by the structure variables by any function anywhere in their scope.
  • 7. STRUCTURES AND CLASSES IN C++  C++ supports all the features of structures as defined in C.  In C++, a structure can have both variables and functions as members.  It can declare some of its members as ‘private’.  In C++, the structure names are stand-alone and can be used like any other type names. student A; // C++ declaration
  • 8. STRUCTURES AND CLASSES IN C++  By default the members of a class are private, while, by default, the members of a structure are public. continue…
  • 9. CLASS  A class is a way to bind the data and its associated functions together.  It allows the data( and functions ) to be hidden, if necessary, from external use.  A CLASS specification has two parts:  Class Declaration  Class Function Definitions Describes the type and scope of its members Describes how the class functions are implemented
  • 10. CLASS DECLARATION class class_name { private : variable declarations; function declarations; public : variable declarations; function declarations; }; The class declaration is similar to a struct declaration.
  • 11. CLASS DECLARATION  The body of a class is enclosed within braces and terminated by a semicolon.  The class body contains the declaration of variables and functions.  These functions and variables collectively called class members. continue… class class_name { private : variable declarations; function declarations; public : variable declarations; function declarations; };
  • 12. CLASS DECLARATION  Members grouped into two sections :  Private - visibility labels  Public  The keyword are followed by colon. continue… class class_name { private : variable declarations; function declarations; public : variable declarations; function declarations; };
  • 13. CLASS DECLARATION  The class members that have been declared as private can be accessed only from within the class.  Public members can be accessed from outside the class also.  Keyword private is optional. By default, the members of a class are private. continue… class class_name { private : variable declarations; function declarations; public : variable declarations; function declarations; };
  • 14. CLASS DECLARATION class class_name { private : variable declarations; function declarations; public : variable declarations; function declarations; };  The variables declared inside the class are known as data members.  and the functions are known as member functions.  Only the member functions can have access to the private data members and private functions. continue…
  • 15. CLASS DECLARATION class class_name { private : variable declarations; function declarations; public : variable declarations; function declarations; };  The public members (both functions and data) can be accessed from outside the class.  The binding of data and functions together into a single class-type variable is referred to as encapsulation. continue…
  • 16. CLASS DECLARATION continue… Data Functions Private area Data Functions Public area No entry to private area X Entry allowed to public area Data hiding in CLASS
  • 17. CLASS EXAMPLE class item { int number; // variable declaration float cost; // private by default public : void getdata( int a, float b); // function declaration void putdata( void ); // using prototype };
  • 18. CLASS EXAMPLE  Give meaningful names to classes.  Names become the new type identifier that can be used to declare instances of that class type.  The class item contains two data members and two member functions. continue… class item { int number; floatcost; public : void getdata(int a, float b); void putdata(void); };
  • 19. CLASS EXAMPLE  The data members are private by default  While both the functions are public by declaration.  The functions are declared but not defined.  Actual function definition will appear later in the program. continue… class item { int number; floatcost; public : void getdata(int a, float b); void putdata(void); };
  • 20. CLASS EXAMPLE continue… class item { int number; floatcost; public : void getdata(int a, float b); void putdata(void); }; Class : ITEM DATA number cost ……… FUNCTIONS getdata( ) putdata( ) ……… Representation of a class
  • 21. CREATING OBJECTS Once a class has been declared, we can create variables of that type by using the class name. item x ; // create a variable x of type item. In C++, the class variables are known as objects. item x, y, z ; // declare more than one objects in one statement
  • 22. CREATING OBJECTS  The declaration of an object is similar to that of any basic type.  The necessary memory space is allocated to an object at this stage.  Class specification, like a structure, provides only a template and does not create any memory space for the objects. continue…
  • 23. CREATING OBJECTS  Object can also be created when a class is defined by placing their names immediately after the closing brace. class item { …… …… …… } x, y, z ; continue…
  • 24. ACCESSING CLASS MEMBERS  The private data of a class can be accessed only through the member functions of that class. object-name . function-name ( actual- arguments); In our example, although x is an object of the type item to which number belongs, the number can be accessed only through a member function and not by the object directly.
  • 25. DEFINING MEMBER FUNCTIONS  Member functions can be defined in two places:  Outside the class definition.  Inside the class definition.
  • 26. DEFINING MEMBER FUNCTIONS  Outside the Class Definition  Member functions that are declared inside a class have to be defined separately outside the class.  Their definitions are very much like the normal functions.  They should have a function header and a function body.  An important difference between a member function and a normal function is that a member function incorporates a membership “identity label” in the header. continue… This label tells the compiler which class the function belongs to.
  • 27. DEFINING MEMBER FUNCTIONS  Outside the Class Definition return-type class-name : : function-name (argument declaration) { Function body }  The membership label class-name : : tells the compiler that the function function-name belongs to the class class-name.  The scope of the function is restricted to the class-name specified in the header line. continue…
  • 28. DEFINING MEMBER FUNCTIONS  Inside the Class Definition  Replace the function declaration with the definition of the function inside the class.  When a function is defined inside a class, it is treated as an inline function.  All the restrictions and limitations that apply to an inline function are also applicable to the functions defined inside a class. continue…
  • 29. ADVANTAGE OF INLINE FUNCTIONS  It speeds up your program by avoiding function calling overhead.  It save overhead of variables push/pop on the stack, when function calling happens.  It save overhead of return call from a function.  It increases locality of reference by utilizing instruction cache.  By marking it as inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining)
  • 30. DISADVANTAGE OF INLINE FUNCTION  It increases the executable size due to code expansion.  C++ inlining is resolved at compile time. Which means if you change the code of the inlined function, you would need to recompile all the code using it to make sure it will be updated.  When used in a header, it makes your header file larger with information which users don’t care.  As mentioned above it increases the executable size, which may cause thrashing in memory. More number of page fault bringing down your program performance.  Sometimes not useful for example in embedded system where large executable size is not preferred at all due to memory constraints.
  • 31. USABILITY OF INLINE FUNCTION  Use inline function when performance is needed.  Use inline function over macros.  Prefer to use inline keyword outside the class with the function definition to hide implementation details.
  • 32. MAKING AN OUTSIDE AND INSIDE FUNCTIONS
  • 33. MAKING AN OUTSIDE AND INSIDE FUNCTIONS
  • 34. MAKING AN OUTSIDE FUNCTIONS INLINE  The member functions defined outside a class can be made inline by using the qualifier inline in the header line of function definition. class item { …… …… public : void getdata (int a, float b); }; inline void item : : getdata (int a, float b) { number = a ; cost = b ; }
  • 35.
  • 36.
  • 37. OUTPUT  Enter first value: 45  Enter second value: 15  Addition of two numbers: 60  Difference of two numbers: 30  Product of two numbers: 675  Division of two numbers: 3
  • 38. NESTING OF MEMBER FUNCTIONS  The member function of a class can be called only by an object of that class using a dot operator.  But a member function can be called by using its name inside another member function of the same class.  This is known as nesting of member functions.
  • 39. PRIVATE MEMBER FUNCTIONS  Private member functions can be created for making them to be hidden.  A private member function can only be called by another function that is a member of its class.  Even an object cannot invoke a private function using the dot operator.
  • 40. PRIVATE MEMBER FUNCTIONS class product { int code ; float stock ; void read ( void ) ; public : void update( void ) ; void display( void ) ; }; If p1 is an object, then p1.read ( ) is illegal. However, the function read( ) can be called by any of the public functions of this class. void product : : update ( void) { read ( ) ; }; continue…
  • 41. ARRAYS WITHIN A CLASS The arrays can be used as member variables in a class. const int size = 10; class matrix { int mat [ size ] ; public: void getval ( ) ; void putval ( ) ; };
  • 42. MEMORY ALLOCATION FOR OBJECTS  The member functions are created and placed in the memory space only once when they are defined.  Since all the objects belongs to that class use the same member functions, no separate space is allocated for member functions when the objects are created.  Only space for member variables is allocated separately for each object.  Separate memory locations for the objects are essential, because the member variables hold different data values for different objects.
  • 43. STATIC DATA MEMBERS  A data member of a class can be qualified as static.  Characteristics of static member variables:  It is initialized to zero when the first object of its class is created. No other initialization is permitted.  Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.  It is visible only within the class, but its lifetime is the entire program.  Static variables are normally used to maintain values common to the entire class.
  • 44. STATIC DATA MEMBERS  The type and scope of each static member variable must be defined outside the class definition.  This is because the static data members are stored separately rather than as a part of an object.  Since they are associated with class itself rather than with any class object, they are also known as class variables. continue…
  • 45. STATIC DATA MEMBERS  Static variables are like non-inline member functions as they are declared in a class declaration and defined in the source file.  While defining a static variable, some initial value can also be assigned to the variable.  type class-name : : static-variable = initial value; continue…
  • 46.
  • 47.
  • 48. STATIC MEMBER FUNCTIONS  Like static member variable, we can also have static member functions.  Properties of member functions:  A static function can have access to only other static members ( functions or variables ).  A static member function can be called using the class name ( instead of its objects ) as:  class-name : : function-name;
  • 49.
  • 50.
  • 51. ARRAYS OF OBJECTS  Arrays of variables that are of type class are called arrays of objects. class employee { char name [30]; float age; public: void getdata (void); void putdata (void); }; employee manager [5]; employee worker [25]; • The array manager contains five objects, viz manager[0], manager[1], manager[2], manager[3] & manager[4]. • Array of objects behave like any other array. • manager [i]. putdata( ); to execute the putdata( ) member function of the ith element of the array manager.
  • 52. OBJECTS AS FUNCTION ARGUMENTS  An object can be used as a function argument like any other data type.  Two ways:  A copy of the entire object is passed to the function. ( Pass- by-Value)  Only the address of the object is transferred to the function. (Pass-by-Reference)  The pass-by-reference method is more efficient since it requires to pass only the address of the object and not the entire object.
  • 53. OBJECTS AS FUNCTION ARGUMENTS  An object can also be passed as an argument to a non- member function.  Such functions can have access to the public member functions only through the objects passed as arguments to it.  These functions cannot have access to the private data members. continue…
  • 54. FRIENDLY FUNCTIONS  The private members can not be accessed from outside the class.  A non-member function can not have an access to the private data of a class.  However ……. ?
  • 55. FRIENDLY FUNCTIONS  C++ allows a common function to be made friendly with more than one classes, thereby allowing the function to have access to the private data of these classes.  Such a function need not be a member of these classes.  To make an outside function friendly to a class, we have to simply declare this function as a friend of the class. continue…
  • 56. FRIENDLY FUNCTIONS  The function declaration should be preceded by the keyword friend.  The function is defined elsewhere in the program like a normal C++ function.  The function definition does not use either the keyword friend or the scope operator : :. class employee { --- --- public : --- --- friend void it_cal (void); } continue…
  • 57. FRIENDLY FUNCTIONS  The functions that are declared with the keyword friend are known as friend function.  A function can be declared as a friend in any number of classes.  A friend function, although not a member function, has full access right to the private members of the class. continue…
  • 58. FRIENDLY FUNCTIONS Special Characteristics:  It is not in the scope of the class to which it has been declared as friend.  Since it is not in the scope of the class, it cannot be called using the object of the class.  It can be invoked like a normal function without the help of any object. continue…
  • 59. FRIENDLY FUNCTIONS Special Characteristics:  Unlike member functions, it cannot access the member names directly and has to use an object name and dot membership operator with each member name.  It can be declared either in the public or private part of a class without affecting its meaning.  Usually, it has objects as arguments. continue…
  • 60. FRIENDLY FUNCTIONS Member function of one class can be friend functions of another class. In such cases, they are defined using the scope resolution operator as: continue…
  • 61. FRIENDLY FUNCTIONS class X { … … int fun1 ( ); … }; class Y { … … friend int X : : fun1 ( ); … }; continue…
  • 62. FRIENDLY FUNCTIONS We can also declare all the member functions of one class as the friend functions of another class. In such cases, the class is called a friend class. class Z { … … friend class X ; … }; continue…
  • 63. RETURNING OBJECTS Like a function can receive objects as arguments, it can also return objects.
  • 64. CONST MEMBER FUNCTIONS If a member function does not alter any data in the class, then it is called a const member function. void mul (int, int) const ; void get_balance( ) const ; The qualifier const is appended to the function prototypes ( in both declaration and definition). The compiler will generate an error message if such functions try to alter the data values.
  • 65. POINTER TO MEMBERS  It is possible to take the address of a member of a class and assign it to a pointer.  The address of a member can be obtained by applying the operator & to a fully qualified class member name.  A class member pointer can be declared using the operator : : * with the class name.
  • 66. POINTER TO MEMBERS We can define a pointer to the member m as follows: int A : : * pm = &A : : m; A : : * “pointer-to-member of A class”. &A : : m means “address of the m member of A class”. class A { private : int m ; public : void show( ) ; } ; continue…
  • 67. POINTER TO MEMBERS The dereferencing operator .* is used when the object itself is used with the member pointer. The dereferencing operator ->* is used to access a member when we use pointers to both the object and the member. class A { int m ; public : void show( ) ; } ; A a ; int A : : * pm = & A : : m ; A * pa = & a ; continue…
  • 68. POINTER TO MEMBERS The dereferencing operator .* is used when the object itself is used with the member pointer. The dereferencing operator ->* is used to access a member when we use pointers to both the object and the member. class A { int m ; public : void show( ) ; } ; A a ; int A : : * pm = & A : : m ; A * pa = & a ; continue… To refer the member m a .* pm To refer the member m pa -> * pm
  • 69. POINTER TO MEMBERS We can also design pointers to member functions which, then, can be invoked using the dereferencing operators in the main. (object-name . * pointer-to-member function) ( ) (pointer-to-object -> * pointer-to-member function) ( ) continue… The precedence of ( ) is higher than that of . * and -> * , so the parentheses are necessary.
  • 70. LOCAL CLASSES Classes can be defined and used inside a function or a block. Such classes are called local classes. Local classes can be used global variables and static variables but can not use automatic variables. The global variables should be used with the scope operator ( : : ). They cannot have static data members and member functions must be defined inside the local classes.