SlideShare a Scribd company logo
C STRUCTURES
REVISITED
• One of the unique facilities provided by C language is
structure
• It is used to group logically related data items
• It is user defined data-type and once structure type is
defined, variables of that type can be created
2
LIMITATIONS OF C
STRUCTURE
• They don’t allow data hiding
• Structure members can be accessed using structure
variable by any function any where in the scope
• C does not allow the structure data type to be treated like
built-in data type
3
AN INTRODUCTION TO CLASSES
• A class is a building block of OOP. It is the way to bind the data
and its logically related functions together.
• An abstract data type that can be treated like any other built in
data type.
CLASS DEFINITION
Class head class name_of_class.
Class body {
data members;
member functions;
};
SPECIFYING A CLASS
Specification of a class consists of two parts:
• Class declaration
• Function definition
Syntax:
class class_name
{
private:
variable declarations
function declarations
public:
variable declarations
function declarations
};
6
CONTD…
The key feature of OOP is data hiding.
Generally, data within a class is made private and the
functions are public.
So, the data will be safe from accidental manipulations, while
the functions can be accessed from outside the class
However, it is not necessary that the data must be private
and functions public
7
CREATING OBJECTS
Once a class has been declared, variables of that type can be
created by using the class name as data-type
Test t1; //memory for t1 is allocated
This statement creates a variable t1 of type test.
The class variables are known as objects
t1 is called object of class test.
More than one object of a class can be created
8
CONTD…
Objects are also called instances of class
Objects can be created as follows:
class employee
{
int id;
char name[20];
public:
void getname();
void putname();
}e1, e2, e3;
9
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.
ACCESSING CLASS
MEMBERS
The class members are accessed using dot operator
However it works only for the public members
The dot operator is called member access operator
The general format is:
class-object.class-member;
Eg: e1.getdata();
The private data and functions of a class can be accessed only
through the member functions of that class
11
DEFINING MEMBER
FUNCTIONS
Member function can be defined in two ways
Outside the class
Inside the class
The code for the function body would be identical in both the
cases i.e perform same task irrespective of the place of
definition
12
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
--------------
}
13
CONTD…
Eg:
class A
{
int x, y;
public:
void getdata (); // function declaration inside the
class
}
void A :: getdata()
{
cin>>x>>y; // function body
}
14
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
Hence, all the restrictions that apply to inline function, will
also apply here
15
Eg:
class A
{
int a, b;
public:
void getdata()
{
cin>>a>>b;
}
};
Here, getdata() is defined inside the class.
So, it will act like an inline function
A function defined outside the class can
also be made ‘inline’ by using the qualifier
‘inline’ in the header line of a function
definition
16
NESTED MEMBER
FUNCTIONS
An object of the class using dot operator, generally, calls a
member function of a class
However, 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”
17
PRIVATE MEMBER
FUNCTIONS
Member functions are in general made public
But in some cases, 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
Objects of the class cannot invoke it using dot operator
18
Eg:
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
}
If a1 is an object of A, then the following statement is
not valid
a1.read();
This is because, read is a private member function
which cannot be called using object and dot operator
19
STATIC MEMBER
FUNCTION
In a class, functions can also be declared as static
Properties of static functions are:
• They can access only other STATIC members(functions or
variables) declared in the same class
• They can be called using class name ( instead of its object)
• Eg: class_name::function_name
20
class A
{
int no;
static int count; //static member
public:
void set_no()
{
count++;
no=count;
}
void put_no()
{
cout<<“ No is:” <<no;
}
static void put_count() //static member function
accessing static member
{
cout<<endl<<“Count:”
<<count;
}
};
Int A::count;
main()
{
A a1, a2;
a1.set_no();
a2.set_no();
A::put_count();
a1.set_no();
a2.set_no();
A::put_count();
a1.put_no();
a2.put_no();
21
ARRAY OF OBJECTS
Array can be created of any datatype
Since a class is also a user defined data-type, array of
objects can be created
Eg: a class Employee is specified. If we have to keep records
of 20 employees in an organization having two departments,
then instead of creating 20 separate variables, we can create
array of objects as follows:
Employee dept1[10];
Employee dept2[10];
22
MEMORY ALLOCATION FOR
OBJECTS
Memory space for object is allocated when they are declared and
not when the class is specified
The member functions are created and placed in the memory only
once, when they are defined as a part of a class specification
All the objects belonging to the particular class will use same
member functions when objects are created
However, the data members will hold different values for different
object, so, space for data member is allocated separately for each
object
23
OBJECTS AS FUNCTION
ARGUMENTS
Like any other variable, objects can also be passed to the
function, as an argument
There are two ways of doing this
• Pass by value
• Pass by reference
In the pass by value, the copy of the object is passed to the
function
So, the changes made to the object inside the function do not
affect the actual object
24
Eg:
class height
{
int feet, inches;
public:
void get_height()
{
cout<<“Enter height in feet and
inches”;
cin>>feet>>inches;
}
void put_height()
{
cout<<“Feet:”<<feet;
cout<<and inches:”<<inches;
}
void sum(height, height);
};
void height:: sum(height h1, height h2)
{
inches= h1.inches+h2.inches;
feet=inches/12;
inches=inches%12;
feet=feet+h1.feet+h2.feet;
}
int main()
{
height h1, h2, h3;
h1.get_height();
h2.get_height();
h3.sum(h1, h2);
cout<<“Height 1: ” ; h1.put_height();
cout<<“Height 2: ” ; h2.put_height();
cout<<“Height 3: ” ; h3.put_height();
return(0);
}
25
FRIEND FUNCTIONS AND
FRIEND CLASSES
friend declarations
• To declare a friend function
• Type friend before the function prototype in the class
that is giving friendship
friend int myFunction( int x );
should appear in the class giving friendship
• To declare a friend class
• Type friend class Classname in the class that is
giving friendship
• if ClassOne is granting friendship to ClassTwo,
friend class ClassTwo;
• should appear in ClassOne's definition
1 // Fig. 7.5: fig07_05.cpp
2 // Friends can access private members of a class.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 // Modified Count class
9 class Count {
10 friend void setX( Count &, int ); // friend declaration
11 public:
12 Count() { x = 0; } // constructor
13 void print() const { cout << x << endl; } // output
14 private:
15 int x; // data member
16 };
17
18 // Can modify private data of Count because
19 // setX is declared as a friend function of Count
20 void setX( Count &c, int val )
21 {
22 c.x = val; // legal: setX is a friend of Count
23 }
24
25 int main()
26 {
27 Count counter;
28
29 cout << "counter.x after instantiation: ";
30 counter.print();
Changing private variables
allowed.
31 cout << "counter.x after call to setX friend function: ";
32 setX( counter, 8 ); // set x with a friend
33 counter.print();
34 return 0;
35 }
counter.x after instantiation: 0
counter.x after call to setX friend function: 8

More Related Content

What's hot

Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 augshashank12march
 
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
Venugopalavarma Raja
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
Rokonuzzaman Rony
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
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
Govt. P.G. College Dharamshala
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Marlom46
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Anil Kumar
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
NainaKhan28
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
ThamizhselviKrishnam
 
Inheritance
InheritanceInheritance
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
Adeel Rasheed
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
Dev Chauhan
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
Class and object
Class and objectClass and object
Class and object
prabhat kumar
 

What's hot (20)

Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
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
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
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++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Functions, classes & objects in c++
Functions, classes & objects in c++Functions, classes & objects in c++
Functions, classes & objects in c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Class and object
Class and objectClass and object
Class and object
 

Similar to Object and class presentation

chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
study material
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
SURBHI SAROHA
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
Shweta Shah
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
Atif Khan
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Class and object
Class and objectClass and object
Class and object
Prof. Dr. K. Adisesha
 
Class object
Class objectClass object
Class object
Dr. Anand Bihari
 
C++ classes
C++ classesC++ classes
C++ classes
imhammadali
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Asfand Hassan
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
Rai University
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
Rai University
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
urvashipundir04
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
rumanatasnim415
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
Mohammed Saleh
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
Durga Devi
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
Mohamad Al_hsan
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-exampleDeepak Singh
 

Similar to Object and class presentation (20)

chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
ccc
cccccc
ccc
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 
Class and object
Class and objectClass and object
Class and object
 
Class object
Class objectClass object
Class object
 
C++ classes
C++ classesC++ classes
C++ classes
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
 
Bca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objectsBca 2nd sem u-2 classes & objects
Bca 2nd sem u-2 classes & objects
 
Mca 2nd sem u-2 classes & objects
Mca 2nd  sem u-2 classes & objectsMca 2nd  sem u-2 classes & objects
Mca 2nd sem u-2 classes & objects
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 

Recently uploaded

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 

Recently uploaded (20)

Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 

Object and class presentation

  • 1.
  • 2. C STRUCTURES REVISITED • One of the unique facilities provided by C language is structure • It is used to group logically related data items • It is user defined data-type and once structure type is defined, variables of that type can be created 2
  • 3. LIMITATIONS OF C STRUCTURE • They don’t allow data hiding • Structure members can be accessed using structure variable by any function any where in the scope • C does not allow the structure data type to be treated like built-in data type 3
  • 4. AN INTRODUCTION TO CLASSES • A class is a building block of OOP. It is the way to bind the data and its logically related functions together. • An abstract data type that can be treated like any other built in data type.
  • 5. CLASS DEFINITION Class head class name_of_class. Class body { data members; member functions; };
  • 6. SPECIFYING A CLASS Specification of a class consists of two parts: • Class declaration • Function definition Syntax: class class_name { private: variable declarations function declarations public: variable declarations function declarations }; 6
  • 7. CONTD… The key feature of OOP is data hiding. Generally, data within a class is made private and the functions are public. So, the data will be safe from accidental manipulations, while the functions can be accessed from outside the class However, it is not necessary that the data must be private and functions public 7
  • 8. CREATING OBJECTS Once a class has been declared, variables of that type can be created by using the class name as data-type Test t1; //memory for t1 is allocated This statement creates a variable t1 of type test. The class variables are known as objects t1 is called object of class test. More than one object of a class can be created 8
  • 9. CONTD… Objects are also called instances of class Objects can be created as follows: class employee { int id; char name[20]; public: void getname(); void putname(); }e1, e2, e3; 9
  • 10. 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.
  • 11. ACCESSING CLASS MEMBERS The class members are accessed using dot operator However it works only for the public members The dot operator is called member access operator The general format is: class-object.class-member; Eg: e1.getdata(); The private data and functions of a class can be accessed only through the member functions of that class 11
  • 12. DEFINING MEMBER FUNCTIONS Member function can be defined in two ways Outside the class Inside the class The code for the function body would be identical in both the cases i.e perform same task irrespective of the place of definition 12
  • 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 -------------- } 13
  • 14. CONTD… Eg: class A { int x, y; public: void getdata (); // function declaration inside the class } void A :: getdata() { cin>>x>>y; // function body } 14
  • 15. 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 Hence, all the restrictions that apply to inline function, will also apply here 15
  • 16. Eg: class A { int a, b; public: void getdata() { cin>>a>>b; } }; Here, getdata() is defined inside the class. So, it will act like an inline function A function defined outside the class can also be made ‘inline’ by using the qualifier ‘inline’ in the header line of a function definition 16
  • 17. NESTED MEMBER FUNCTIONS An object of the class using dot operator, generally, calls a member function of a class However, 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” 17
  • 18. PRIVATE MEMBER FUNCTIONS Member functions are in general made public But in some cases, 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 Objects of the class cannot invoke it using dot operator 18
  • 19. Eg: 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 } If a1 is an object of A, then the following statement is not valid a1.read(); This is because, read is a private member function which cannot be called using object and dot operator 19
  • 20. STATIC MEMBER FUNCTION In a class, functions can also be declared as static Properties of static functions are: • They can access only other STATIC members(functions or variables) declared in the same class • They can be called using class name ( instead of its object) • Eg: class_name::function_name 20
  • 21. class A { int no; static int count; //static member public: void set_no() { count++; no=count; } void put_no() { cout<<“ No is:” <<no; } static void put_count() //static member function accessing static member { cout<<endl<<“Count:” <<count; } }; Int A::count; main() { A a1, a2; a1.set_no(); a2.set_no(); A::put_count(); a1.set_no(); a2.set_no(); A::put_count(); a1.put_no(); a2.put_no(); 21
  • 22. ARRAY OF OBJECTS Array can be created of any datatype Since a class is also a user defined data-type, array of objects can be created Eg: a class Employee is specified. If we have to keep records of 20 employees in an organization having two departments, then instead of creating 20 separate variables, we can create array of objects as follows: Employee dept1[10]; Employee dept2[10]; 22
  • 23. MEMORY ALLOCATION FOR OBJECTS Memory space for object is allocated when they are declared and not when the class is specified The member functions are created and placed in the memory only once, when they are defined as a part of a class specification All the objects belonging to the particular class will use same member functions when objects are created However, the data members will hold different values for different object, so, space for data member is allocated separately for each object 23
  • 24. OBJECTS AS FUNCTION ARGUMENTS Like any other variable, objects can also be passed to the function, as an argument There are two ways of doing this • Pass by value • Pass by reference In the pass by value, the copy of the object is passed to the function So, the changes made to the object inside the function do not affect the actual object 24
  • 25. Eg: class height { int feet, inches; public: void get_height() { cout<<“Enter height in feet and inches”; cin>>feet>>inches; } void put_height() { cout<<“Feet:”<<feet; cout<<and inches:”<<inches; } void sum(height, height); }; void height:: sum(height h1, height h2) { inches= h1.inches+h2.inches; feet=inches/12; inches=inches%12; feet=feet+h1.feet+h2.feet; } int main() { height h1, h2, h3; h1.get_height(); h2.get_height(); h3.sum(h1, h2); cout<<“Height 1: ” ; h1.put_height(); cout<<“Height 2: ” ; h2.put_height(); cout<<“Height 3: ” ; h3.put_height(); return(0); } 25
  • 26. FRIEND FUNCTIONS AND FRIEND CLASSES friend declarations • To declare a friend function • Type friend before the function prototype in the class that is giving friendship friend int myFunction( int x ); should appear in the class giving friendship • To declare a friend class • Type friend class Classname in the class that is giving friendship • if ClassOne is granting friendship to ClassTwo, friend class ClassTwo; • should appear in ClassOne's definition
  • 27. 1 // Fig. 7.5: fig07_05.cpp 2 // Friends can access private members of a class. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // Modified Count class 9 class Count { 10 friend void setX( Count &, int ); // friend declaration 11 public: 12 Count() { x = 0; } // constructor 13 void print() const { cout << x << endl; } // output 14 private: 15 int x; // data member 16 }; 17 18 // Can modify private data of Count because 19 // setX is declared as a friend function of Count 20 void setX( Count &c, int val ) 21 { 22 c.x = val; // legal: setX is a friend of Count 23 } 24 25 int main() 26 { 27 Count counter; 28 29 cout << "counter.x after instantiation: "; 30 counter.print(); Changing private variables allowed.
  • 28. 31 cout << "counter.x after call to setX friend function: "; 32 setX( counter, 8 ); // set x with a friend 33 counter.print(); 34 return 0; 35 } counter.x after instantiation: 0 counter.x after call to setX friend function: 8