Oop Unit 2
Oop Unit 2
Oop Unit 2
object_name.function_name(actual_arguments);
eg.-- x.getdata(100,12.25);
x.rollno=10;
#include<iostream.h>
#include<conio.h>
class student{
int rollno;
void get();
public:
int marks;
void put();
};
void student::get(){
Rollno=5;
Marks=88;
}
void student::put(){
get();
cou<<rollno;
cout<<marks;
}
void main(){
student s;
s.rollno=5; //error
s.marks=88;
s.get(); //error
s.put()
}
● Members declared as public can be accessed by
objects.
● A private member function can be called by
another function that is a member of its class.
● An object cannot invoke a private function
using the dot operator
Prg1- WAP to declare a class 'student' having data
members as stud_id, name, roll_no. Accept and display
this data for one object.
#include<iostream.h>
#include<conio.h>
class student{
int stud_id, roll_no;
char name[30];
public:
void getdata();
void putdata();
};
void student::getdata(){
cout<<”Enter student ID, roll number and name”;
cin>>stud_id>>roll_no>>name;
}
void student :: putdata(){
cout<<”\n Student ID”<<stud_id;
cout<<”\n Student Roll Number”<<roll_no;
cout<<”\n Student Name”<<name;
}
void main(){
student s;
clrscr();
s.getdata();
s.putdata();
getch();
}
Prg 2-- WAP to declare a class 'Journal' having data members as
journal_nm, price, ISSN_No. Accept this data for two objects
and display the name of journal having greater price.
#include<iostream.h>
#include<conio.h>
class journal{
public:
char journal_nm[20];
int price, ISSN_No;
void accept(){
cout<<”Enter journal name, price and ISSN number”;
cin>>journal_nm>>price>>ISSN_No;
}
};
void main(){
journal j1,j2;
clrscr();
j1.accept();
j2.accept();
if(j1.price>j2.price)
cout<<j1.journal_nm<<”has greater price”<<j1.price;
else
cout<<j2.journal_nm<<”has greater price”<<j2.price;
getch();
}
J1---Enter journal name, price and ISSN number
A
10
112
J2---Enter journal name, price and ISSN number
B
20
445
B has greater price 20
Prg 3-- wap to declare a class 'Day' having data members as
hours, min, sec. Accept min from user convert in
appropriate hours, min and sec. Display it for one object
of a class.
#include<iostream.h>
#include<conio.h>
class day{
int min;
public:
void accept(){
cout<<”enter minute”;
cin>>min;
}
void display(){
cout<<”seconds=”<<min*60;
cout<<”minute=”<<min;
cout<<”hours=”<<min/60;
}};
void main(){
day d;
clrscr();
d.accept();
d.display();
getch();
}
Nesting of member function--
A member function can be called by using its name inside
another function of the same class. This is known as
nesting of member functions.
eg.-- #include<iostream.h>
#include<conio.h>
class set{
int m,n;
public:
void input();
void display();
int largest();
};
void set::input(){
cout<<”Input value for m and n”;
cin>>m>>n;
}
void set::display(){
cout<<”Largest value=”<<largest();
}
int set::largest(){
if(m>n)
return(m);
else
return(n);
}
void main(){
set A;
A.input();
A.display();
getch();
}
Memory allocation for object
part of an object.
●Since they are associated with the class itself rather than
with any class object, they are also known as class variable.
rectangle::rectangle(float h, float w)
{
height = h;
width = w;
xpos = 0;
ypos = 0;
Like other member function there is no need to
call constructor explicitly. It is invoked
automatically each time the object of its class is
created.
Every class having at least one constructor
defined in it. If you do not define any constructor
in the class then compiler will automatically
create a constructor inside the class and
assigns default value (0) to the data member of
the class.
Types of constructor--
1) Default constructor
2) Parameterised constructor
3) Copy constructor
4) Dynamic constructor
Default Constructor.
A constructor that accepts no parameters is
called the default constructor.
If default constructor isnot defined in program
then the compiler supplies a default
constructor.
Constructor declaration
class student
{
int rollno;
char name[20];
public:
student();
}
Constructor definition--
student::student(){
rollno=0;
name=”Priya”;
}
Constructor calling--
void main(){
student s;
....
}
Constructor can be defined inside class as shown
below:
class Rectangle
{
int Height, Width;
public:
Rectangle()
{
Height = 1;
Width = 1;
}
};
Constructor can be defined outside class as
shown below:
class Rectangle
{
int Height, Width;
public:
Rectangle();
}
Rectangle :: Rectangle()
{
Height = 1;
Width = 1;
}
Now when you create an object of the class
Rectangle as shown below:
Rectangle R1;
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
float per;
char name[20];
public:
student();
void put();
};
student::student(){
strcpy(name,”abc”);
per=98.8;
}
void student::put(){
cout<<”Name of student”<<name;
cout<<”percentage”<<per;
}
void main(){
student s;
s.put();
getch();
}
OR
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
float per;
char name[20];
public:
student(){
strcpy(name,”abc”);
per=98.8;
}
void put(){
cout<<”Name of student”<<name;
cout<<”percentage”<<per;
}
};
void main(){
student s;
s.put();
getch();
}
Parameterized Constructor
It is possible to pass arguments to constructor
functions. These arguments help to initialize
an object when it is created.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
float per;
char name[20];
public:
student(char nm[20], float p){
strcpy(name,nm);
per=p;
}
void put();
};
void student::put(){
cout<<”Name of student”<<name;
cout<<”percentage”<<per;
}
void main(){
student s(“abc”,90);
s.put();
getch();
}
Prg 2—Define a class to represent a bank account which
includes:
Data members: name, acc_no, acc_type, bal_amt and
constructor to assign initial value.
Member function: to deposit an amount, to withdraw an
amount and to display the name and balance.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank_account{
int acc_no, bal_amt;
char name[20], acc_type[20];
public:
bank_account(char n[30], int an, char at[20], int ba);
void deposit();
void withdrawl();
void display();
bank_account::bank_account(char n[30], int an, char
at[20], int ba){
acc_no=an;
bal_amt=ba;
strcpy(name, n);
strcpy(acc_type, at);
}
void bank_account::deposit(){
int dep_amt;
cout<<”\n Enter deposit amount”;
cin>>dep_amt;
bal_amt=bal_amt+dep_amt;
cout<<”\n Amount=”<<dep_amt<<”is deposited to an
account”<<acc_no<<”\n available balance”<<bal_amt;
}
void bank_account::withdrawl(){
int wd_amt;
cout<<”Enter withdrawl amount”;
cin>>wd_amt;
bal_amt=bal_amt-wd_amt;
cout<<”\n Amount”<<wd_amt<<”is withdrawl from an
account”<<acc_no<<”\n available balance ”<<bal_amt;
}
void bank_account::display(){
cout<<”\n name”<<name<<”available balance”<<bal_amt;
}
void main(){
bank_account x(“ABC”, 765, “Fix”, 10000);
clrscr();
x.deposit();
x.withdrawl();
x.display();
getch();
Constructor with default argument
It is possible to define constructor with default
argument.
For eg- student(int rn, float per=80);
#include<iostream.h>
#include<conio.h>
class salary{
float BASIC, TA, DA, HRA;
public:
salary(float, float, float, float);
void display();
};
salary::salary(float ta, float basic, float da=200, float
hra=400){
BASIC=basic;
TA=(BASIC*ta)/100;
DA=(BASIC*da)/100;
HRA=(BASIC*hra)/100
}
void salary::display(){
cout<<”\n Salary of employee:”<<( BASIC+TA+DA+HRA);
}
void main(){
salary s(110, 6000);
s.display();
getch();
}
Prg2--Define a class college which will contain member
variable as rollno, name and course. Write a program using
constructor with default values as “Information Technology”
for course. Accept this data for two objects of class and
display the data.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student{
int rollno;
char name[30], course[30];
public:
student(char str[30]=”Information Technology”);
void accept();
void display();
};
student::student(char str[30]){
strcpy(course,str);
}
void student::accept(){
cout<<”Enter rollno and name:”;
cin>>rollno>>name;
}
void student::display(){
cout<<”Roll Number=”<<rollno;
cout<<”name=”<<name;
cout<<”Course=”<<course; }
void main(){
student s1,s2;
s1.accept();
s2.accept();
s1.display();
s2.display();
getch(); }
Copy constructor
A constructor that accept a reference to its
own class as a parameter which is called as a
copy constructor.
o/p--
ID of A 11
ID of B 11
The statement
code B(A);
Would invoke the second constructor which copies the
values of A into B.
Roll Number=0
Percentage=0
Roll Number=2
Percentage=90
Roll Number=2
Percentage=90
Destructor
Destructor is a member function of the class.
It is called special member function because
the name of the destructor is same as name of
the class but it is preceded by the tilde (~)
sign.
Destructor is used to destroy the object that is
created using constructor.
Like other member function there is no need to
call destructor explicitly. It is invoked
automatically(implicitly) by the compiler upon
exit from the program to clean up storage(free
memory space) that is no longer accessible.