Classes and
Objects
ESPRD1507A02-2015
1. Class :
Class is a user-defined data type which contains data-
member and member functions to approach on these
data.
• Classis like blueprint of object that model real world things.
• Class is also and implementation of ADT (Abstract Data Type).
Advantages:
.Once you have created class for certain , you can reuse that class in any project ( Code
Reusability)
•Class is used for data-abstraction ,data hiding and encapsulation.
There are two categories of Class:
a)Global Class – When the class is called outside the body function .
b)Local Class – When the class is called inside the body function.
Example (Global Class):
#include <iostream>
using namespace std;
//create a class book
class books{
int b_id;//book id
char pub[100];//publication name
public:
//getting input from the user
void getdata()
{
cin>>b_id;
cin>>pub;
}
//display the result
void display()
{
cout<<"Book ID = "<< b_id <<" "<<"Publication = "<<pub;
}
};
int main()
{
books b1;//create an object of a class where b1 is instance variables
b1.getdata();
b1.display();//message or behavior
}
Example (Local Class):
using namespace std;
void testfun()
{
class books
{
int b_id;
char pub[100];
public:
void getdata()
{
cout<<"Insert Book ID";
cin>>b_id;
cout<<"Insert Publication name";
cin>>pub;
}
void display()
{
cout<<"Book ID = "<<b_id;
cout<<"Publication = "<<pub;
}
};
books b1;
b1.getdata();
b1.display();
}
int main()
{
testfun();
return 0;
}
Objects:
Real world entity that has its state and certain behavior is called Object.
Objects is a variable of class data-type which is a run-time instance of a
class.
• State of an object represent all the information held inside (within).
• Characteristics of object is determined by the data members of class.
• Behavior is represented by member functions.
Example
using namespace std;
void testfun()
{
class books
{
int b_id;//member function of a class books
char pub[100];
public:
void getdata()
{
cout<<"Insert Book ID";
cin>>b_id;
cout<<"Insert Publication name";
cin>>pub;
}
void display()
{
cout<<"Book ID = "<<b_id;
cout<<"Publication = "<<pub;
}
};
books b1; // Object of a class books.
b1.getdata();//instance variable of class
b1.display();//displays the result
}
int main()
{
testfun();//function call
return 0;
}
Access mode
The objects communicate by sending message to each other . Objects react
when they receive a message by applying methods on themselves.
Message is a request to object to invoke(use/call) on of its
method.
Object can be defined by three elements:
a)Identity of object.
b)Instance variable
c)Behavior of object
Object unique name is the identity of an object and
the internal state of an object are “instance variables”
and operations are called “message”. Collection of
message determines the interface and behavior of the
object.
Most common operation(methods):
1. Modifier or Set method :
Method that can change state of object. Set method can store the
variables.
2. Selector or Get method:
Method that access the state of an objects but does not make any
chances to its state. Get method can access the information from the class.

Classes and objects

  • 1.
  • 2.
    1. Class : Classis a user-defined data type which contains data- member and member functions to approach on these data. • Classis like blueprint of object that model real world things. • Class is also and implementation of ADT (Abstract Data Type). Advantages: .Once you have created class for certain , you can reuse that class in any project ( Code Reusability) •Class is used for data-abstraction ,data hiding and encapsulation. There are two categories of Class: a)Global Class – When the class is called outside the body function . b)Local Class – When the class is called inside the body function.
  • 3.
    Example (Global Class): #include<iostream> using namespace std; //create a class book class books{ int b_id;//book id char pub[100];//publication name public: //getting input from the user void getdata() { cin>>b_id; cin>>pub; } //display the result void display() { cout<<"Book ID = "<< b_id <<" "<<"Publication = "<<pub; } }; int main() { books b1;//create an object of a class where b1 is instance variables b1.getdata(); b1.display();//message or behavior }
  • 4.
    Example (Local Class): usingnamespace std; void testfun() { class books { int b_id; char pub[100]; public: void getdata() { cout<<"Insert Book ID"; cin>>b_id; cout<<"Insert Publication name"; cin>>pub; } void display() { cout<<"Book ID = "<<b_id; cout<<"Publication = "<<pub; } }; books b1; b1.getdata(); b1.display(); } int main() { testfun(); return 0; }
  • 5.
    Objects: Real world entitythat has its state and certain behavior is called Object. Objects is a variable of class data-type which is a run-time instance of a class. • State of an object represent all the information held inside (within). • Characteristics of object is determined by the data members of class. • Behavior is represented by member functions. Example using namespace std; void testfun() { class books { int b_id;//member function of a class books char pub[100]; public: void getdata() { cout<<"Insert Book ID"; cin>>b_id; cout<<"Insert Publication name"; cin>>pub; } void display() { cout<<"Book ID = "<<b_id; cout<<"Publication = "<<pub; } }; books b1; // Object of a class books. b1.getdata();//instance variable of class b1.display();//displays the result } int main() { testfun();//function call return 0; } Access mode
  • 6.
    The objects communicateby sending message to each other . Objects react when they receive a message by applying methods on themselves. Message is a request to object to invoke(use/call) on of its method. Object can be defined by three elements: a)Identity of object. b)Instance variable c)Behavior of object Object unique name is the identity of an object and the internal state of an object are “instance variables” and operations are called “message”. Collection of message determines the interface and behavior of the object.
  • 7.
    Most common operation(methods): 1.Modifier or Set method : Method that can change state of object. Set method can store the variables. 2. Selector or Get method: Method that access the state of an objects but does not make any chances to its state. Get method can access the information from the class.