0% found this document useful (0 votes)
82 views13 pages

CP II Unit V

The document discusses classes and objects in C++. [1] A class is a user-defined data type that collects together data members and member functions. [2] An object is an instance of a class that stores its own set of data members. [3] Classes allow data and functions to be bundled together through encapsulation, while objects are instantiated from classes and can access class members and functions.

Uploaded by

virat gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views13 pages

CP II Unit V

The document discusses classes and objects in C++. [1] A class is a user-defined data type that collects together data members and member functions. [2] An object is an instance of a class that stores its own set of data members. [3] Classes allow data and functions to be bundled together through encapsulation, while objects are instantiated from classes and can access class members and functions.

Uploaded by

virat gautam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Reference: - Object Oriented Programming With C++ by E.

Balagurusamy

Classes & Objects


Class
- Collection of similar type of object is called as class.
- It is a way to bind data and its associated functions together.
- It helps in hiding data and functions from external use.
- It is user define data type.
- Class is declared by the use of “class” keyword.
- Every class has properties associated with it.
Object
- If object is member of a class then it is called an instance, but instance of anything
is not object, for example instance of structure in C is not object.
- Object is conceptual entity that can be identifiable.
- Anything which shows behavior [whenever time comes] is called object.
- Object is an entity which have physical boundary.
- Objects have characteristics [state, behavior & identity]

Relationship between class & objects.

- An instance of a class can be related to any number of instances of other class


known as multiplicity of the relation.

Type Example
One to One 1 Country : 1 President
One to Many 1 Country : Many States
Many to One Many Student : 1 Teacher
Many to Many Many Student : Many Teacher

- Two objects of same class have relationship with each other.


- HAS-A and IS-A are terms used to describe object relationships.
- Composition (HAS-A) simply mean use of instance variables that are references
to other objects.
- Example:-
1. A lake HAS-A: fish, plant, and a frog.
 This means that our Lake class contains references to a: fish, frog,
and plant object.
2. House HAS-A Kitchen, Room & Bathroom
- Is A is nothing but use of inheritance.
- Example:-
1. A trout IS-A fish, so we extend [inherit] a fish class to make a trout class.
2. Apple IS-A Fruit, we can inherit Fruit class for Apple.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Specifying Class:-

- Generally, a class specification has two parts:


1. Class declaration.
 It describes type and scope of class members
2. Class function definition.
 It describes how the class functions are implemented.
- Syntax:
class class_name{

private:
variable declarations:
function declarations;
public:
variable declarations:
function declarations;
};

- The keyword class specifies, that what follows is an abstract data of type
class_name.
- The body of class enclosed within braces and terminated by a semicolon.
- The class body contains variables and functions, called class members.
- Class members usually grouped under two sections viz public & private.
- The keywords public and private are visibility modifier and followed by colon.
- By default, the members of class are private.
- The variable declared in class is called data members and functions are known as
member functions.
- Member functions can have access to the private data members and member
functions.
- Public members can be accessed from outside of class.\
- The binding of data and functions together into a single class type variable is
referred to as encapsulation.

Access Specifiers/ Visibility Modifiers.

- Public, private and protected are access specifiers.


- It specifies the manner in which data can be accessed.
- By default is private. And it not accessible out of class.
- Access specifiers are used for data hiding.
- Public member are accessible out of class definitions.
- Protected members are accessible in Base and derived class.

Creating Objects

- Once a class has been declared, we can create variable of that type.
- Example:-
Item x;
- x is variable of type Item.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

- In C++, the class variables are known as objects.


- Therefore x is called an object of type Item.
- Objects can also be declared when a class is defined by placing their name after
the closing brace.
- Syntax:-
class class_name{
// class body
}obj_name;
- This practice is seldom followed because we would like to declare the objects
close to the place where they are used and not at the time of class definition.

Defining the Member functions:

- Member functions can be defined in two places


1. Outside the class definition.
 When function is define outside of class definition, class_name::
requires before function name, to tell compiler that, this is not
normal function, it is member function of class.
 The syntax for a member function definition outside the class
definition is :

return_type name_of_the_class::function_name (argument list){

body of function
}
2. Inside the class definition.
 When function is defined inside class , it is treated as an inline
function.
 Normally, small functions are defined inside the class definition.

Declaration of Objects as Instances Of A Class

- The objects of a class are declared after the class definition.


- One must remember that a class definition does not define any objects of its type, but
it defines the properties of a class.
- For utilizing the defined class, we need variables of the class type.
- For example,
Largest ob1,ob2;
o object declaration will create two objects ob1 and ob2 of largest class type.
- As mentioned earlier, in C++ the variables of a class are known as objects.
- These are declared like a simple variable i.e., like fundamental data types.
- In C++, all the member functions of a class are created and stored when the class is
defined and this memory space can be accessed by all the objects related to that class.
- Memory space is allocated separately to each object for their data members.
- Member variables store different values for different objects of a class

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Accessing Members from Objects.

- After defining a class and creating a class variable i.e., object we can access the data
members and member functions of the class.
- Because the data members and member functions are parts of the class, we must
access these using the objects we created.
- For functions are parts of the class, we must access these using the objects we created.

Simple class & object Example

class Item{
int a; By default, private
float b;
public:
void read( ); Function declaration
void display( );
};

void Ìtem::read( ){
Member function outside of class definition
cout<< “Enter two number\n”;

cin>>a>>b;
}
Function definitions
void Item::display( ){
cout<< “a=”<<a<< “ b=”<<b;

main( ){
Item I; Object Creation…
i.read( );
i.display( ); Accessing Class Members…

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Question: Create a class student having data members name, rollno & branch of
student. Also declare two methods i.e. getData( ) & display( ) for taking input & display
the same. Write a complete C++ code for display the information of a single student.
class Student{
char *name, *branch;
int rollno;

public:
void getData(int rno, char *nm, char *brch){
rollno=rno;
name=nm;
branch=brch;
}
void display( ){
cout<<“Roll No is ”<<rollno<<endl;
cout<<“Name is ”<<name<<endl;
cout<<“Branch is ”<<branch;
}

};
main( ){
Student s;
s.getData(1,“Mugdha”, “Computer”);
s.display( );

Memory Allocation for Objects

- Memory space for objects is allocated when they are declared and not when
the class is specified.
- The member functions are created and placed in memory space only once
when they are defined as part of a class specification.
- Since all objects belonging to the class use same member functions, no
separate space is allocated for member functions when the objects are created.
- Only space for member variables is allocated separately for each object.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Static Data Member:

- It is generally used to store value common to the whole class.


- The static data member differs from an ordinary data member in the following ways :
1. Only a single copy of the static data member is used by all the objects.
2. It can be used within the class but its lifetime is the whole program.
- For making a data member static, we require :
(a) Declare it within the class.
(b) Define it outside the class
- The definition outside the class is a must.
- It is initialized to zero when first object is created.
- It is visible only with in the class, but its lifetime is the entire program.
- Example:-

class Item{
int a;
float b;
static int n; Static member declaration…
public:
void read( );
void display( );
};

void Ìtem::read( ){
cout<< “Enter two number\n”;
cin>>a>>b;
n++;
}
void Item::display( ){
cout<< “a=”<<a<< “ b=”<<b;
n++:
}
void staticMem( ){
cout<< “n = ”<<n;
}
int Item::n; Static Member definition.
As it is static automatically initialized to zero.
main( ){
Item I;
i.read( );
i.display( );
i.staticMem( );

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Static Member Function:

- A static member function can access to only other static members of a class.
- A static member function can be called using class name as follows:
Class_name::static_function_name( );

- Examples:-

class Item{
int a;
float b;
static int n; Static member declaration…
public:
void read( );
void display( );
};

void Ìtem::read( ){
cout<< “Enter two number\n”;
cin>>a>>b;
n++;
}
void Item::display( ){
cout<< “a=”<<a<< “ b=”<<b;
n++:
}
static void staticMem( ){ Static member function
cout<< “n = ”<<n;
}
int Item::n; Static Member definition.
As it is static automatically initialized to zero.
main( ){
Item I;
i.read( );
i.display( );
Item::staticMem( ); Accessing Static function…

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Arrays of Objects

- We can have arrays of variables that are the type class, such variables are
called array of objects.
- Arrays of objects are stored inside the memory in same way as
multidimensional array.
- Member functions are stored separately and will be used by all the objects
[each index of array].
- Example:-
class Student{
char *name, *branch;
int rollno;

public:
void getData( ){
cout<< “Enter roll no , name and branch of student\n”
cin>>rollno;
cin>>name;
cin>>branch;
}
void display( ){
cout<<“Roll No is ”<<rollno<<endl;
cout<<“Name is ”<<name<<endl;
cout<<“Branch is ”<<branch;
}

};
main ( ){
Student s[5]; // array of objects to work with 5 student records….
cout<< “Enter 5 student info\n ”
for(int i=0;i<5;i++)
s[i].getData( );

cout<< “Details of all students is\n”


for(i=0;i<5;i++)
s[i].display( );

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Objects as Function Arguments

- Like any other data type, an object may be used as function argument.
- This can be done in two ways:
o A copy of entire object is passed to the function [call by value].
o Only the address of object is transferred to the function [call by
reference].
- Example:
- Program to calculate age of a person.

#include<iostream.h>
#include<conio.h>
class Date{
int d,m,y;
public:
//Date();
Date(int,int,int);
void age(Date,Date);
};
Date::Date(int dd, int mm, int yy){
d=dd;
m=mm;
y=yy;
}

void Date::age(Date tod, Date dob){


int ay,am,ad;
ay=tod.y-dob.y;
if(tod.m<dob.m){ Object as
ay-=1; arguments
tod.m+=12;
}
am=tod.m-dob.m;
if(tod.d<dob.d){
am-=1;
tod.d+=30;
}
ad=tod.d-dob.d;
cout<<ay<<"-years "<<am<<"-months "<<ad<<" -days";
}

main(){
int td,tm,ty,bd,bm,by;
clrscr();
cout<<"Enter Todays Date\n" ;
cin>>td>>tm>>ty;
cout<<"Enter Date of birth\n";
cin>>bd>>bm>>by;
Date t(td,tm,ty),b(bd,bm,by);
t.age(t,b);
getch();
}

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Returning Objects

- Function can also return objects.


- Examples:-

#include<iostream.h>
#include<conio.h>
class Date{
public:
int d,m,y;
public:
//Date();
Date(int,int,int);
void age(Date,Date);
}; Returning
Date::Date(int dd, int mm, int yy){
d=dd; Object
m=mm;
y=yy;
}

Date Date::age(Date tod, Date dob){


Date a;
a.y=tod.y-dob.y;
if(tod.m<dob.m){
a.y-=1; Object as
tod.m+=12; arguments
}
a.m=tod.m-dob.m;
if(tod.d<dob.d){
a.m-=1;
tod.d+=30;
}
a.d=tod.d-dob.d;
return a;
}

main(){
int td,tm,ty,bd,bm,by;
clrscr();
cout<<"Enter Todays Date\n" ;
cin>>td>>tm>>ty;
cout<<"Enter Date of birth\n";
cin>>bd>>bm>>by;
Date t(td,tm,ty),b(bd,bm,by);
t=t.age(t,b);
cout<<t.y<<"-years "<<t.m<<"-months "<<t.d<<" -days";

getch();
}

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

FRIEND CLASSES

- In C++ , a class can be made a friend to another class.


- For example,
class B; // forward declaration of the class TWO
class A{
public:
friend class B;
};
class B{
//body
};
- B is friend of A..
- Now from class B, all the member of class A can be accessed.

Nested Class:-

- When one class is defined inside other then it is called nested class.
- The nested class can access the data members of outside class.
- Data members of nested class can be accessed from main( ), if they are public using
objects.

class A{
public:
void funA( ){
cout<< “FunA( )”<<endl;
}
class B{
public:
void funB( ){
cout<< “FunB( )”<<endl;
}
};
};
main( ){
A a; //object of outer class.
A::B b ; // object of inner class
a.funA( );
b.funB( );
}

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Local Class

- Local class can be defined inside the function only. And its members are not
accessible from other functions.
- Member functions of local class must be defined within local class.
- Can not contain static data members.
- Can access global variable.
- One local class can access data member of another local class, provided they are in
same function.
- Example:
Void fun( ){
class Local{
public:
void display( ){
cout<< “Wel Come to Local class…\n”;
}
};
Local object;
object.display( );
}
main( ){
fun( );
}

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi


Reference: - Object Oriented Programming With C++ by E. Balagurusamy

Difference between Class and Object

Class Object
Doesn’t exist in reality Exists in reality.
The scope of class is persistent throughout The object can be created and destroyed as
the program. per requirement.
Have attributes and properties. Shows behavior.
A class has unique name. Objects have different name for same class.

Difference between Class and Structure

Class Structure
By default members are private By default members are public.
Can be inherited Cannot be inherited
Class contains data and function as members. Structure contains data as members.
Instance of a class is object Instance of structure is variable.
Constructors are required Constructors are not required.

Computer Programming-II Notes Prepared By: - Dhananjay L. Joshi

You might also like