!
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)
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

Presentation on class and object in Object Oriented programming.

  • 1.
  • 2.
    Presentation on classand 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? Aclass is kind of a blueprint or template that refer to the methods and attributes that will be in each object
  • 5.
    Specifying a class Specificationof a class consists of two parts: – Class declaration – Function definition
  • 6.
    • The classspecification 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 accessspecifies (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 definingobjects 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 accessingdata 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’sname 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: wemay 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 friendfunction 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 functionthat 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:
  • 18.