SlideShare a Scribd company logo
1 of 22
CLASSES AND OBJECTS
Class:
A Class is user define data type to implement an abstract object.
Abstract mean to hide the details.A class is a combinition of data
and function.
 Data are called data member and function are called member
function.
 . A Class is a blueprint for objects.
 When a class is defined, no memory is allocated.
 A class definition begins with the keyword class.
 The body of the class is contained within a set of braces{ } and semicolon with ending
braces.
{
…………..
..…………..
……………..
}:
Class class_name
{
some data
some
function
};
Class
declaring
Class
body
Class
end
But remember class is never complete without access specifier.
 The commands that are used to specify the access of class members are known as access specifier
 There are three access specifier which are:
 Private
 Public
 Protected
Above three access specifier are keyword which is written as it is but having different function.
 Keyword private makes data and functions private and keyword public
makes data and functions public.
 Private data and functions are accessible inside that class only whereas,
public data and functions are accessible both inside and outside the class. This
feature in OOP is known as data hiding. If programmer mistakenly tries to
access private data outside the class, compiler shows error which prevents the
misuse of data.
 Generally, data are private and functions are public
Class student
{
Private:
int roll;
Char name[10];
Public:
Void get_data();
Void put_data();
};
 Data members and member functions can be accesses by using member
operator(.).
 The data member can be accessed as:
object_name.data_memeber;
 And member function can be accessed as:
object.function name
Class sum
int a ,b , t;
Public:
Void get_data()
{
cout<<“enter value of a and b”;
cin>>a>>b;
}
Void put_data()
{
t=a+b;
cout<<“sum of a and b is:”<<t;
}
};
Int main()
{
sum obj;
Obj.get_data();
Obj.put_data();
getche();
return 0:
}
If we want to define member function outside the class
we can used it with the help of scope resolution
operator(::).
Class student void student::show_data()
{ {
Private: cout<<“name of student is
int roll; :”<<name;
Char name[10]; cout<<“roll no of student
Public: is:”<<roll no;
Void get_data(); }
Void show_data(); int main()
}; {
void student::get_data() ……………
{ ……………….
cout<<“enter name”; getche();
cin>>name; return 0;
cout<<“enter roll no”; }
cin>>roll no;
}
 We can also declare a class inside a function when class declared inside
a function is called local class
 A local is only known to that function and unknown inside it.
 Due to these restriction local class is not so popular in c++.
 Object is an abstraction of real world entity.
 An object is an instance of a class.
 An object is a class variable.
 It can be uniquely identified by its name.
 Syntax for declaring object is as fallows:
Object_name.member function(parameter list);
Class sum
int a ,b , t;
Public:
Void get_data()
{
cout<<“enter value of a and b”;
cin>>a>>b;
}
Void put_data()
{
t=a+b;
Cout<<“sum of a and b is:”<<t;
}
};
int main()
{
sum obj;
obj.get_data();
obj.put_data();
getche();
return 0:
}
This can be done by two ways.
 A copy of entire object is passed to the function(i.e. pass by value)
 Only the address of the object is transferred to the function(i.e. pass by
reference).
A program to demonstrate passing objects by value to a member function of the same
class
#Include<iostream.h>
 class weight
 {
 int kilogram;
 int gram;
 public:
 void getdata ();
 void putdata ();
 void sum_weight (weight,weight) ;
 } ;
 void weight :: getdata()
{
 cout<<"/nKilograms:";
 cin>>kilogram;
 cout<<"Grams:";
 cin>>gram;
void weight :: putdata ()
{
cout<<kilogram<<" Kgs. and"<<gram<<" gros.n";
}
void weight :: sum_weight(weight wl,weight w2)
{
gram = wl.gram + w2.gram;
kilogram=gram/1000;
gram=gram%1000;
kilogram+=wl.kilogram+w2.kilogram;
}
int main ()
{
weight wl,w2 ,w3;
cout<<"Enter weight in kilograms and gramsn";
cout<<"n Enter weight #1" ;
wl.getdata();
cout<<" n Enter weight #2" ;
w2.getdata();
w3.sum_weight(wl,w2);
cout<<"/n Weight #1 = ";
wl.putdata();
cout<<"Weight #2 = ";
w2.putdata();
cout<<"Total Weight = ";
w3.putdata();
return 0;
}
The output of the program is
Enter weight in kilograms and grams
Enter weight #1
Kilograms: 12
Grams: 560
Enter weight #2
Kilograms: 24
Grams: 850
Weight #1 = 12 Kgs. and 560 gms.
Weight #2 = 24 Kgs. and 850 gms.
Total Weight = 37 Kgs. and 410 gms.
Classes and Objects in C++ Explained

More Related Content

What's hot

C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsMayank Jain
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++jehan1987
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 
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
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IEduardo Bergavera
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 augshashank12march
 
C++ Returning Objects
C++ Returning ObjectsC++ Returning Objects
C++ Returning ObjectsJay Patel
 

What's hot (20)

C++ classes
C++ classesC++ classes
C++ classes
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using 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
 
Chapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part IChapter 4 - Defining Your Own Classes - Part I
Chapter 4 - Defining Your Own Classes - Part I
 
C++ oop
C++ oopC++ oop
C++ oop
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
Class object
Class objectClass object
Class object
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ Returning Objects
C++ Returning ObjectsC++ Returning Objects
C++ Returning Objects
 

Viewers also liked

project report of social networking web sites
project report of social networking web sitesproject report of social networking web sites
project report of social networking web sitesGyanendra Pratap Singh
 
Social Networking Project
Social Networking ProjectSocial Networking Project
Social Networking Projectjessduff44
 
Social Networking Project (website) full documentation
Social Networking Project (website) full documentation Social Networking Project (website) full documentation
Social Networking Project (website) full documentation Tenzin Tendar
 

Viewers also liked (6)

ZAINAB
ZAINABZAINAB
ZAINAB
 
Pbl.doc
Pbl.docPbl.doc
Pbl.doc
 
social networking site
social networking sitesocial networking site
social networking site
 
project report of social networking web sites
project report of social networking web sitesproject report of social networking web sites
project report of social networking web sites
 
Social Networking Project
Social Networking ProjectSocial Networking Project
Social Networking Project
 
Social Networking Project (website) full documentation
Social Networking Project (website) full documentation Social Networking Project (website) full documentation
Social Networking Project (website) full documentation
 

Similar to Classes and Objects in C++ Explained

Similar to Classes and Objects in C++ Explained (20)

class c++
class c++class c++
class c++
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
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
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
class and objects
class and objectsclass and objects
class and objects
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Implementation of oop concept in c++
Implementation of oop concept in c++Implementation of oop concept in c++
Implementation of oop concept in c++
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
3 functions and class
3   functions and class3   functions and class
3 functions and class
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Class and object
Class and objectClass and object
Class and object
 
oop lecture 3
oop lecture 3oop lecture 3
oop lecture 3
 
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
 
Class and object
Class and objectClass and object
Class and object
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
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
 

Classes and Objects in C++ Explained

  • 2. Class: A Class is user define data type to implement an abstract object. Abstract mean to hide the details.A class is a combinition of data and function.  Data are called data member and function are called member function.  . A Class is a blueprint for objects.  When a class is defined, no memory is allocated.
  • 3.
  • 4.  A class definition begins with the keyword class.  The body of the class is contained within a set of braces{ } and semicolon with ending braces. { ………….. ..………….. …………….. }: Class class_name { some data some function }; Class declaring Class body Class end
  • 5.
  • 6. But remember class is never complete without access specifier.  The commands that are used to specify the access of class members are known as access specifier  There are three access specifier which are:  Private  Public  Protected Above three access specifier are keyword which is written as it is but having different function.
  • 7.  Keyword private makes data and functions private and keyword public makes data and functions public.  Private data and functions are accessible inside that class only whereas, public data and functions are accessible both inside and outside the class. This feature in OOP is known as data hiding. If programmer mistakenly tries to access private data outside the class, compiler shows error which prevents the misuse of data.  Generally, data are private and functions are public
  • 8. Class student { Private: int roll; Char name[10]; Public: Void get_data(); Void put_data(); };
  • 9.  Data members and member functions can be accesses by using member operator(.).  The data member can be accessed as: object_name.data_memeber;  And member function can be accessed as: object.function name
  • 10. Class sum int a ,b , t; Public: Void get_data() { cout<<“enter value of a and b”; cin>>a>>b; } Void put_data() { t=a+b; cout<<“sum of a and b is:”<<t; } }; Int main() { sum obj; Obj.get_data(); Obj.put_data(); getche(); return 0: }
  • 11. If we want to define member function outside the class we can used it with the help of scope resolution operator(::).
  • 12. Class student void student::show_data() { { Private: cout<<“name of student is int roll; :”<<name; Char name[10]; cout<<“roll no of student Public: is:”<<roll no; Void get_data(); } Void show_data(); int main() }; { void student::get_data() …………… { ………………. cout<<“enter name”; getche(); cin>>name; return 0; cout<<“enter roll no”; } cin>>roll no; }
  • 13.  We can also declare a class inside a function when class declared inside a function is called local class  A local is only known to that function and unknown inside it.  Due to these restriction local class is not so popular in c++.
  • 14.  Object is an abstraction of real world entity.  An object is an instance of a class.  An object is a class variable.  It can be uniquely identified by its name.
  • 15.  Syntax for declaring object is as fallows: Object_name.member function(parameter list);
  • 16. Class sum int a ,b , t; Public: Void get_data() { cout<<“enter value of a and b”; cin>>a>>b; } Void put_data() { t=a+b; Cout<<“sum of a and b is:”<<t; } }; int main() { sum obj; obj.get_data(); obj.put_data(); getche(); return 0: }
  • 17.
  • 18. This can be done by two ways.  A copy of entire object is passed to the function(i.e. pass by value)  Only the address of the object is transferred to the function(i.e. pass by reference).
  • 19. A program to demonstrate passing objects by value to a member function of the same class #Include<iostream.h>  class weight  {  int kilogram;  int gram;  public:  void getdata ();  void putdata ();  void sum_weight (weight,weight) ;  } ;  void weight :: getdata() {  cout<<"/nKilograms:";  cin>>kilogram;  cout<<"Grams:";  cin>>gram;
  • 20. void weight :: putdata () { cout<<kilogram<<" Kgs. and"<<gram<<" gros.n"; } void weight :: sum_weight(weight wl,weight w2) { gram = wl.gram + w2.gram; kilogram=gram/1000; gram=gram%1000; kilogram+=wl.kilogram+w2.kilogram; } int main () { weight wl,w2 ,w3; cout<<"Enter weight in kilograms and gramsn"; cout<<"n Enter weight #1" ; wl.getdata(); cout<<" n Enter weight #2" ; w2.getdata(); w3.sum_weight(wl,w2); cout<<"/n Weight #1 = "; wl.putdata(); cout<<"Weight #2 = "; w2.putdata(); cout<<"Total Weight = "; w3.putdata(); return 0; }
  • 21. The output of the program is Enter weight in kilograms and grams Enter weight #1 Kilograms: 12 Grams: 560 Enter weight #2 Kilograms: 24 Grams: 850 Weight #1 = 12 Kgs. and 560 gms. Weight #2 = 24 Kgs. and 850 gms. Total Weight = 37 Kgs. and 410 gms.