CP II Unit V
CP II Unit V
Balagurusamy
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
Specifying Class:-
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.
Creating Objects
- Once a class has been declared, we can create variable of that type.
- Example:-
Item x;
- x is variable of type Item.
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.
- 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.
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…
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 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.
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( );
- 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…
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( );
- 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;
}
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();
}
Returning Objects
#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;
}
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();
}
FRIEND CLASSES
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( );
}
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( );
}
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.
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.