SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
!
Presentation on class and object
Mashkura Farhat; ID:180110
Enam Khan; ID:180107
Sadia Afreen Farha ; ID:180116
Presented by Presented to
Prafulla Kumar Saha
Lecturer
Department of Computer Science & Engineering
Daffodil Institute of IT(DIIT)
Presentation on class and object in Object Oriented programming.
What is class?
A class is kind of a blueprint
or template that refer to the
methods and attributes that
will be in each object
Specifying a class
Specification of a class consists of two parts:
– Class declaration
– Function definition
• The class specification starts with a
keyword “class”, followed by a class-
name.
• The class-name is an identifier
• The body of class is enclosed within
braces and terminated by a
semicolon
• The functions and variables are
collectively called class members.
• The variables are called data
members while the functions are
called member functions
• The two keywords private and
public are termed as access-specifiers
or visibility labels.
• They are followed by colon
class class_name
{
private:
variable declarations
function declarations
public:
variable declarations
function declarations
};
Characteristics of access specifies (private, public and
protected
• Private section of a class can have both data members and member functions,
usually data members are made private for data security.
• It is not mandatory that private section has to declared first in the class and then
the public section.
• If no member access specifies is specified then by default the members are
private for the class.
• There may be any number of private, public or protected section in a class
declaration.
• Protected specifies is used for declaring the class members which can be
accessed by its own class and its derived class
What is objects?
• Object: A class provides the blueprints for objects, so basically an object is
created from a class. Object can be called as an instance of a class. When class is
defined, no memory is allocated but when it is instantiated memory will be
allocated
• The definition of class does not cause any storage to be allocated. Storage is only
allocated when object of class type is defined
• Following statements declare two objects of class Box:
• Box Box1;
• Box Box2;
Syntax of defining objects of a
class
• Class className objectName
• Class : keyword
• ClassName : user defined class name
• User defined object name
• Objects can be created as follows:
class employee
{
int id;
char name[20];
public:
void getname();
void putname();
}e1, e2, e3;
Syntax of accessing data members of
class
•ObjectName : Data member
•ObjectName : user defined object
name
•“.” : member access
operator
•Data member : data member of a class.
Example:
class A
{
int x, y;
void fu1();
public:
int z;
void fu2();
};
--------------------------
A obj1;
obj1.x=0; //generates error since x is private and can be accessed only though member functions
obj1.z=0; //valid
obj1.fu1(); //generates error since fu1() is private
obj1.fu2(); //valid
Member Function
Member function’s name is visible outside the class.
 It can be defined inside or outside the class.
 It can have access to private, public and protected data
members of its class, but cannot access private data
members of another class
Outside the Class:
• In this approach, the member functions are only declared inside the class,
whereas its definition is written outside the class.
• General form:
Return-type class-name::function-name(argument-list)
{ ------------
------------ // function body
------------
}
Inside the class:
• Function body can be included in the class itself by replacing function declaration by function definition.
• If it is done, the function is treated as an inline function.
Example:
class A
{
int a, b;
public:
void getdata()
{
cin>>a>>b;
}
};
Nested Member Functions: 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” .
Example: #include<iostream.h>
class addition
{
int a, b, sum;
public:
void read();
void show();
int add();
};
void addition::read()
{
cout<<“enter a, and b”;
cin>>a>>b;
}
void addition::add()
{
return(a+b);
}
void addition ::show()
{
sum=add(); // nesting of function
. cout<<endl<<“sum of a and b is :” <<sum;
}
main()
{
addition a1; a1.read();
a1.show();
return(0);
}
Output:
Enter a and b: 2 3
Sum of a and b is: 5
Private Member Functions:
we may need to make a function a private to hide them from outside world . Private member
functions can only be called by another function that is a member of its class.
Example:
class A
{
int a, b;
void read(); //private member function
public: void update();
void write();
};
void A :: update()
{
read(); //called from update() function. no object used.
}
Friend
function
• A friend function of a class is defined outside that class' scope but it has the right to access all private
and protected members of the class. Even though the prototypes for friend functions appear in the
class definition, friends are not member functions.
• A friend can be a function, function template, or member function, or a class or class template, in which
case the entire class and all of its members are friends.
• To declare a function as a friend of a class, precede the function prototype in the class definition with
keyword friend as follows −
class Box {
double width;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
A member function that is defined inside it’s class member list, is called an
inline member function.
Example:
static inline void swap(int *m, int *n)
{
int tmp = *m;
*m = *n;
*n = tmp;
}
Inline function:
Thank You

More Related Content

PPTX
multiplexer and d-multiplexer
PPT
Atomul
PPS
Virtual memory
PPTX
Microprocessor chapter 9 - assembly language programming
PDF
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PPT
4 Classes & Objects
PDF
Implementation of oop concept in c++
multiplexer and d-multiplexer
Atomul
Virtual memory
Microprocessor chapter 9 - assembly language programming
classandobjectunit2-150824133722-lva1-app6891.ppt
4 Classes & Objects
Implementation of oop concept in c++

Similar to Presentation on class and object in Object Oriented programming. (20)

PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PPTX
Classes and objects
PPTX
Classes and objects
PPT
Class and object in C++
PPTX
C++ppt. Classs and object, class and object
PPT
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
PPT
classes data type for Btech students.ppt
PPTX
class c++
PPTX
Class and objects
PPT
Object and class presentation
PPT
static member and static member fumctions.ppt
PPT
Class objects oopm
PDF
Class and object
PDF
Chapter 7 C++ As OOP
PDF
Introduction to C++ Class & Objects. Book Notes
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
PPTX
Classes and objects till 16 aug
PPTX
Introduction to Class a deep analysisadfas
PPTX
Class and object
PPTX
Class and object
cpp class unitdfdsfasadfsdASsASass 4.ppt
Classes and objects
Classes and objects
Class and object in C++
C++ppt. Classs and object, class and object
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
classes data type for Btech students.ppt
class c++
Class and objects
Object and class presentation
static member and static member fumctions.ppt
Class objects oopm
Class and object
Chapter 7 C++ As OOP
Introduction to C++ Class & Objects. Book Notes
22 scheme OOPs with C++ BCS306B_module1.pdf
Classes and objects till 16 aug
Introduction to Class a deep analysisadfas
Class and object
Class and object
Ad

Recently uploaded (20)

PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
PPTX
How to Manage Loyalty Points in Odoo 18 Sales
PPTX
ACUTE NASOPHARYNGITIS. pptx
PPTX
How to Manage Bill Control Policy in Odoo 18
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Sunset Boulevard Student Revision Booklet
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
How to Manage Global Discount in Odoo 18 POS
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
English Language Teaching from Post-.pdf
PPTX
Onica Farming 24rsclub profitable farm business
PPTX
IMMUNIZATION PROGRAMME pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
PDF
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
PPTX
Introduction and Scope of Bichemistry.pptx
PDF
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Skill Development Program For Physiotherapy Students by SRY.pptx
How to Manage Loyalty Points in Odoo 18 Sales
ACUTE NASOPHARYNGITIS. pptx
How to Manage Bill Control Policy in Odoo 18
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Sunset Boulevard Student Revision Booklet
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Open Quiz Monsoon Mind Game Final Set.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
How to Manage Global Discount in Odoo 18 POS
102 student loan defaulters named and shamed – Is someone you know on the list?
English Language Teaching from Post-.pdf
Onica Farming 24rsclub profitable farm business
IMMUNIZATION PROGRAMME pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
LDMMIA Reiki Yoga S2 L3 Vod Sample Preview
Cell Biology Basics: Cell Theory, Structure, Types, and Organelles | BS Level...
Introduction and Scope of Bichemistry.pptx
What Is Coercive Control? Understanding and Recognizing Hidden Abuse
Ad

Presentation on class and object in Object Oriented programming.

  • 1. !
  • 2. Presentation on class and object Mashkura Farhat; ID:180110 Enam Khan; ID:180107 Sadia Afreen Farha ; ID:180116 Presented by Presented to Prafulla Kumar Saha Lecturer Department of Computer Science & Engineering Daffodil Institute of IT(DIIT)
  • 4. What is class? A class is kind of a blueprint or template that refer to the methods and attributes that will be in each object
  • 5. Specifying a class Specification of a class consists of two parts: – Class declaration – Function definition
  • 6. • The class specification starts with a keyword “class”, followed by a class- name. • The class-name is an identifier • The body of class is enclosed within braces and terminated by a semicolon • The functions and variables are collectively called class members. • The variables are called data members while the functions are called member functions • The two keywords private and public are termed as access-specifiers or visibility labels. • They are followed by colon class class_name { private: variable declarations function declarations public: variable declarations function declarations };
  • 7. Characteristics of access specifies (private, public and protected • Private section of a class can have both data members and member functions, usually data members are made private for data security. • It is not mandatory that private section has to declared first in the class and then the public section. • If no member access specifies is specified then by default the members are private for the class. • There may be any number of private, public or protected section in a class declaration. • Protected specifies is used for declaring the class members which can be accessed by its own class and its derived class
  • 8. What is objects? • Object: A class provides the blueprints for objects, so basically an object is created from a class. Object can be called as an instance of a class. When class is defined, no memory is allocated but when it is instantiated memory will be allocated • The definition of class does not cause any storage to be allocated. Storage is only allocated when object of class type is defined • Following statements declare two objects of class Box: • Box Box1; • Box Box2;
  • 9. Syntax of defining objects of a class • Class className objectName • Class : keyword • ClassName : user defined class name • User defined object name • Objects can be created as follows: class employee { int id; char name[20]; public: void getname(); void putname(); }e1, e2, e3;
  • 10. Syntax of accessing data members of class •ObjectName : Data member •ObjectName : user defined object name •“.” : member access operator •Data member : data member of a class.
  • 11. Example: class A { int x, y; void fu1(); public: int z; void fu2(); }; -------------------------- A obj1; obj1.x=0; //generates error since x is private and can be accessed only though member functions obj1.z=0; //valid obj1.fu1(); //generates error since fu1() is private obj1.fu2(); //valid
  • 12. Member Function Member function’s name is visible outside the class.  It can be defined inside or outside the class.  It can have access to private, public and protected data members of its class, but cannot access private data members of another class
  • 13. Outside the Class: • In this approach, the member functions are only declared inside the class, whereas its definition is written outside the class. • General form: Return-type class-name::function-name(argument-list) { ------------ ------------ // function body ------------ } Inside the class: • Function body can be included in the class itself by replacing function declaration by function definition. • If it is done, the function is treated as an inline function. Example: class A { int a, b; public: void getdata() { cin>>a>>b; } };
  • 14. Nested Member Functions: 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” . Example: #include<iostream.h> class addition { int a, b, sum; public: void read(); void show(); int add(); }; void addition::read() { cout<<“enter a, and b”; cin>>a>>b; } void addition::add() { return(a+b); } void addition ::show() { sum=add(); // nesting of function . cout<<endl<<“sum of a and b is :” <<sum; } main() { addition a1; a1.read(); a1.show(); return(0); } Output: Enter a and b: 2 3 Sum of a and b is: 5
  • 15. Private Member Functions: we may need to make a function a private to hide them from outside world . Private member functions can only be called by another function that is a member of its class. Example: class A { int a, b; void read(); //private member function public: void update(); void write(); }; void A :: update() { read(); //called from update() function. no object used. }
  • 16. Friend function • A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. • A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. • To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows − class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); };
  • 17. A member function that is defined inside it’s class member list, is called an inline member function. Example: static inline void swap(int *m, int *n) { int tmp = *m; *m = *n; *n = tmp; } Inline function: