DEV BHOOMI GROUP OF 
INSTITUTIONS 
SAHARANPUR 
Name : Saurabh Chauhan 
Class : M.C.A. 1ST (2013-15) 
Roll no : 1358614017 
Topic : Classes and Object. 1 
Sub To : Mr. Rakesh Kumar 
Sub By : Saurabh Chauhan
C++ CLASSES 
& 
OBJECT 
WELCOME :
OUTLINE : 
 Introduction of the class : 
 Characteristics of the class : 
 Format of the class : 
 Define a class type : 
 Implementing class methods : 
 Introduction of an Object : 
 Declaration of an object : 
 Reasons for OOP : 
 Thank you : 
3
INTRODUCTION OF THE CLASS : 
 A class is binding the data and methods. 
 A class is a collection of objects of similar type. 
 A class is an object factory (or producer ) that 
produces similar objects. 
 A class is just like an image and model and can say 
a template. 
 A class does not exists physically because it’s a 
image only in our mind 
 But object exists physically because a object is a 
real world entity i.e. a pen , a chair , a desk , a dog , 
a bike , a car ,a men etc 
 The class and object both are sub method of the 
OOP’s methodology 
4
WHY DO WE NEED TO HAVE CLASS ? 
Characteristics of the class : 
 Structures are public by default and classes are 
private by default. 
 Data more secure in the class. 
 Class reduce the complexity. 
 We can easy and well programming, if we use the 
class in our program . 
5
CLASSES IN C++ 
 A class definition begins with the keyword class. 
 The body of the class is contained within a set of 
braces, { } ; (notice the semi-colon). 
class class_name 
{ 
…. 
…. 
…. 
}; 
Any valid 
identifier 
Class body (data member 
+ methods)
CLASSES IN C++ 
 Within the body, the keywords private: and public: 
specify the access level of the members of the 
class. 
 the default is private. 
 Usually, the data members of a class are declared 
in the private: section of the class and the member 
functions are in public: section.
CLASSES IN C++ 
 Format : 
class class_name 
{ 
private: 
… 
… 
… 
public: 
… 
… 
… 
}; 
private members data 
or variables or 
characteristics 
Public members or methods 
Or behavior
DEFINE A CLASS TYPE 
Header 
class class_name 
{ 
access specifier : 
data ; 
access specifier : 
methods ; 
... 
class Rectangle 
{ 
private: 
int width; 
int length; 
public: 
void set(int w, int l); 
int area(); 
}; 9 
}; 
Body
IMPLEMENTING CLASS METHODS 
 Class implementation: writing the code of 
class methods. 
 There are two ways: 
1. Member functions defined outside class 
 Using Binary scope resolution operator (::) 
 “Ties” member name to class name 
 Uniquely identify functions of particular class 
 Different classes can have member functions with 
same name 
 Format for defining member functions 
ReturnType ClassName::MemberFunctionName( 
){ 
… 
}
IMPLEMENTING CLASS METHODS 
2. Member functions(method) defined inside class 
 Do not need scope resolution 
operator, class name; 
class Circle 
{ 
private: 
double r,ar; 
public: 
void area () 
{ 
cin<<r ; 
ar=r*r*3.14; 
} 
cout<<“n a area of circle -”<<ar; 
}; 
Method 
Defined 
inside 
class
// this is a program of area of circle 
// methods (Defined outside class) 
class Circle 
{ 
private: 
double r,ar; 
public: 
void area(); // mehtod 
}; 
void circle ::area 
{ 
cout<<“n enter the radius of the circle :”; 
cin>>r; 
ar=r*r*3.14; 
cout<<“area of the circle :”<<ar; 
} 
Method 
Defined outside class
13 
OBJECT 
 Object: 
 a variable or an instance of a class 
 Objects may represent any entity ,such as a person , a cat 
a chair , a pen , a place , a bank account , a customer , a table 
of data ,etc. 
 for ex ,bike is an object .its characteristics are :its color 
is black ,Its engine is of 500cc ,Its company is Suzuki .Its 
behavior is to starting the engine ,changing the 
gear ,using the break, etc. 
 Declaration of an Object 
 Initiation of an Object
14 
WHAT IS AN OBJECT? 
OBJECT 
Operations 
Data 
set of methods 
(public member functions) 
internal state 
(values of private data members)
EXAMPLE: A “RABBIT” OBJECT 
 You could (in a game, for example) create an object 
representing a rabbit 
 It would have data: 
 How hungry it is 
 How frightened it is 
 Where it is 
 And methods: 
 eat, hide, run, dig
CONCEPT: CLASSES DESCRIBE OBJECTS 
 Every object belongs to (is an instance of) a class 
 An object may have fields, or variables 
 The class describes those fields 
 An object may have methods 
 The class describes those methods 
 A class is like a template, or cookie cutter
17 
DECLARATION OF AN OBJECT 
class Rectangle 
{ 
private: 
int width; 
int length; 
public: 
void set(int w, int l); 
int area(); 
}; 
main() 
{ 
Rectangle r1; 
Rectangle r2; 
r1.set(5, 8); 
cout<<r1.area()<<endl; 
r2.set(8,10); 
cout<<r2.area()<<endl; 
}
REASONS FOR OOP 
1. Simplify programming 
2. Interfaces 
 Information hiding: 
 Implementation details hidden within classes 
themselves 
3. Software reuse 
 Class objects included as members of 
other classes
THANK YOU

C++ And Object in lecture3

  • 1.
    DEV BHOOMI GROUPOF INSTITUTIONS SAHARANPUR Name : Saurabh Chauhan Class : M.C.A. 1ST (2013-15) Roll no : 1358614017 Topic : Classes and Object. 1 Sub To : Mr. Rakesh Kumar Sub By : Saurabh Chauhan
  • 2.
    C++ CLASSES & OBJECT WELCOME :
  • 3.
    OUTLINE : Introduction of the class :  Characteristics of the class :  Format of the class :  Define a class type :  Implementing class methods :  Introduction of an Object :  Declaration of an object :  Reasons for OOP :  Thank you : 3
  • 4.
    INTRODUCTION OF THECLASS :  A class is binding the data and methods.  A class is a collection of objects of similar type.  A class is an object factory (or producer ) that produces similar objects.  A class is just like an image and model and can say a template.  A class does not exists physically because it’s a image only in our mind  But object exists physically because a object is a real world entity i.e. a pen , a chair , a desk , a dog , a bike , a car ,a men etc  The class and object both are sub method of the OOP’s methodology 4
  • 5.
    WHY DO WENEED TO HAVE CLASS ? Characteristics of the class :  Structures are public by default and classes are private by default.  Data more secure in the class.  Class reduce the complexity.  We can easy and well programming, if we use the class in our program . 5
  • 6.
    CLASSES IN C++  A class definition begins with the keyword class.  The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. …. …. }; Any valid identifier Class body (data member + methods)
  • 7.
    CLASSES IN C++  Within the body, the keywords private: and public: specify the access level of the members of the class.  the default is private.  Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.
  • 8.
    CLASSES IN C++  Format : class class_name { private: … … … public: … … … }; private members data or variables or characteristics Public members or methods Or behavior
  • 9.
    DEFINE A CLASSTYPE Header class class_name { access specifier : data ; access specifier : methods ; ... class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; 9 }; Body
  • 10.
    IMPLEMENTING CLASS METHODS  Class implementation: writing the code of class methods.  There are two ways: 1. Member functions defined outside class  Using Binary scope resolution operator (::)  “Ties” member name to class name  Uniquely identify functions of particular class  Different classes can have member functions with same name  Format for defining member functions ReturnType ClassName::MemberFunctionName( ){ … }
  • 11.
    IMPLEMENTING CLASS METHODS 2. Member functions(method) defined inside class  Do not need scope resolution operator, class name; class Circle { private: double r,ar; public: void area () { cin<<r ; ar=r*r*3.14; } cout<<“n a area of circle -”<<ar; }; Method Defined inside class
  • 12.
    // this isa program of area of circle // methods (Defined outside class) class Circle { private: double r,ar; public: void area(); // mehtod }; void circle ::area { cout<<“n enter the radius of the circle :”; cin>>r; ar=r*r*3.14; cout<<“area of the circle :”<<ar; } Method Defined outside class
  • 13.
    13 OBJECT Object:  a variable or an instance of a class  Objects may represent any entity ,such as a person , a cat a chair , a pen , a place , a bank account , a customer , a table of data ,etc.  for ex ,bike is an object .its characteristics are :its color is black ,Its engine is of 500cc ,Its company is Suzuki .Its behavior is to starting the engine ,changing the gear ,using the break, etc.  Declaration of an Object  Initiation of an Object
  • 14.
    14 WHAT ISAN OBJECT? OBJECT Operations Data set of methods (public member functions) internal state (values of private data members)
  • 15.
    EXAMPLE: A “RABBIT”OBJECT  You could (in a game, for example) create an object representing a rabbit  It would have data:  How hungry it is  How frightened it is  Where it is  And methods:  eat, hide, run, dig
  • 16.
    CONCEPT: CLASSES DESCRIBEOBJECTS  Every object belongs to (is an instance of) a class  An object may have fields, or variables  The class describes those fields  An object may have methods  The class describes those methods  A class is like a template, or cookie cutter
  • 17.
    17 DECLARATION OFAN OBJECT class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; main() { Rectangle r1; Rectangle r2; r1.set(5, 8); cout<<r1.area()<<endl; r2.set(8,10); cout<<r2.area()<<endl; }
  • 18.
    REASONS FOR OOP 1. Simplify programming 2. Interfaces  Information hiding:  Implementation details hidden within classes themselves 3. Software reuse  Class objects included as members of other classes
  • 19.