OOPS lab file 178
OOPS lab file 178
ENGINEERING
OBJECT ORIENTED PROGRAMMING
CO 203
LAB FILE
AIM: Using concept of function overloading, compute the area of rectangle, area of
triangle ( by Heron's Formula), area of circle.
THEORY:
Function overloading is a feature of object-oriented programming where two or more
functions can have the same name but different parameters. When a function name is
overloaded with different jobs it is called Function Overloading. In Function
Overloading “Function” name should be the same and the arguments should be
different. Function overloading can be considered as an example of a polymorphism
feature in C++.
SYNTAX:
int add(int a, int b);
int add(int a, int b, int c);
CODE:
#include<iostream>
#include<cmath>
using namespace std;
OUTPUT:
DISCUSSIONS:
At first we declare a function of int type with name “area” and it takes 2 arguments
namely length and breadth.
Next we declare another function of float type having the name “area” which takes 3
arguments namely for 3 sides of a triangle.
We define the functions later in the program and return the respective area to the main
function.
In the function overloading function will call at the time of program compilation. It is
an example of compile-time polymorphism.
Overloaded methods give the flexibility to call a similar method for different types of
data. Function Overloading is done for code reusability, and save efforts.
EXPERIMENT 2
THEORY:
Inline function is a function that is expanded in line when it is called. Inline Functions
are declared when the declared function is not complex and contains simple
statements.When the inline function is called whole code of the inline function gets
inserted or substituted at the point of inline function call. This substitution is
performed by the C++ compiler at compile time. Inline function may increase
efficiency if it is small.
SYNTAX:
inline function_name(argument_list)
{
//function definition should not contain any loops, switch statements, goto statements.
//function should contain simple statements.
}
CODE:
#include<iostream>
#include<cmath>
using namespace std;
class program
{
int x,y;
public:void getdata(int m,int n){
x=m;
y=n;
}
void sum();
void diff();
void multiply();
void div(){
cout<<"\nDivision of numbers: "<<(1.0*x/y);
}
};
OUTPUT:
DISCUSSIONS:
We first create a class “program” and declare various data members. The data members
are by default in the private section of class.
We declare various member functions in the public section of the class. The getdata( )
function is defined within the class only. Member functions for addition,
subtraction, multiplication and division are defined outside the class as inline
functions.
AIM: Declare a class to represent bank account of a customer with following data
members: Name of Depositor, Account Number, Type of Account (S for savings and C
for current account), Balance Amount. The class contains following member functions:
THEORY:
A class is a way to bind the data and its associated functions together. It is a user-
defined data type, which holds its own data members and member functions,
which can be accessed and used by creating an instance of that class. A class is defined
in C++ using keyword class followed by the name of class.
SYNTAX:
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
CODE:
#include <iostream>
using namespace std;
class account
{
char Depositor_Name[50];
int Account_No;
char Account_Type;
float Balance_Amount;
public:
void Input();
void Display();
};
void account::Input()
{
cout<<"Enter the Name of Depositor: "<<endl;
gets(Depositor_Name);
cout<<"Enter the Account Number: "<<endl;
cin>>Account_No;
cout<<"Enter the type of Account: i.e, 'C' for current account and 'S' for savings
account: "<<endl;
cin>>Account_Type;
cout<<"Enter the Balance amount in account:"<<endl;
cin>>Balance_Amount;
}
void account::Display()
{
cout<<"\nName of Depositor: "<<Depositor_Name<<endl;
cout<<"Account number: "<<Account_No<<endl;
cout<<"Type of Account: "<<Account_Type<<endl;
cout<<"Balance Amount: "<<Balance_Amount<<endl;
}
int main(){
account A;
A.Input();
A.Display();
return 0;
}
OUTPUT:
DISCUSSIONS:
A class account is created first, member variables account number, balance amount,
name of depositor etc are declared in private section and member function input,
display are declared in public section of class.
Display: It is used to display all the data members, i.e the account details.
Classes and Objects, by Encapsulation make sure that "sensitive" data is hidden from
users. To achieve this, we must declare class variables/attributes as private (cannot be
accessed from outside the class). If we want others to read or modify the value of a
private member, we can provide public get and set methods.
THEORY:
A data member of a class can be qualified as static. A static member variable has
certain special characteristics. These are:
➢ It is initialized to zero when the first object of its class is created, No other
initialization is permitted.
➢ Only one copy of that member is created for the entire class and is shared by all
the objects of that class, no matter how many objects are created.
➢ It is visible only within the class, but its lifetime is the entire program.
➢ Like static member variable, we can also have static member functions. A
member function that is declared static has the following properties:
➢ A static function can have access to only other static members(functions or
variables) declared in the same class.
SYNTAX:
A static member function can be called using the class name (instead of its objects) as
follows:
class_name::function-name;
CODE:
#include<iostream>
using namespace std;
class test
{
int code;
static int count;
public:
void setcode(){
code=++count;
}
void showcode(){
cout<<"Object Number: "<<code<<endl;
}
static void showcount(){
cout<<"count: "<<count<<endl;
}
};
int test::count;
int main(){
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
OUTPUT:
DISCUSSIONS:
A test class is declared with data members code and count of static type. Member
functions showcode and showcount are declared and defined inside class. The data
member code will keep track of number of objects created and count will show the
value of static variable count.
A static member is shared by all objects of the class. All static data is initialized to zero
when the first object is created, if no other initialization is present. A static member
function can only access static data member, other static member functions and any
other functions from outside the class. Static member functions have a class scope and
they do not have access to the this pointer of the class.
EXPERIMENT 5
AIM: Create a class ‘Employee’ that has employee number and employee names as
data members, getdata( ) and display( ) as member functions. Create an array of object
to show the details of employee.
THEORY:
Class allows the data ( and functions) to be hidden, if necessary, from external use.
When defining a class, we are creating a new abstract data type that can be treated like
any other built-in data type. Generally, a Class specification has two parts:
1. Class declaration
2. Class function definiton
The class declaration describes the type of scope of its members. The class function
definitions describe how the class function are implemented.
SYNTAX:
Class
{
Data members;
Public :
Member functions;
}Object name[array size];
CODE:
#include<iostream>
using namespace std;
class Employee
{
int number;
char name[30];
public:
void getdata();
void display();
};
void Employee::getdata()
{
cout << "Enter Employee number : ";
cin >> number;
cout << "Enter Name : ";
cin >> name;
}
void Employee::display()
{
cout << number << " ";
cout << name << " ";
cout << endl;
}
int main()
{
Employee emp[30]; // This is an array of objects of size 30.
int n, i;
cout << "Enter Number of Employees - ";
cin >> n;
OUTPUT:
DISCUSSIONS:
In this program an array of type class is defined. Various class functions like getdata( ),
display( ) etc are called for each array element separately using for loops. In this way
many objects of a class can be declared using array and this makes program easy to
understand, implemented and debug.
In this example, more than one Employee’s details with an Employee id and name
can be stored.
• Employee emp[30] – This is an array of objects having a maximum limit of
30 Employees.
AIM:Write a program to find the mean of two numbers using friend function.
THEORY:
A friend function is a special function in C++ which in-spite of not being member
function of a class has privilege to access private and protected data of a class.
A friend function is a non member function or ordinary function of a class, which is
declared as a friend using the keyword “friend” inside the class. By declaring a
function as a friend, all the access permissions are given to the function.
SYNTAX:
class class_name
{
data members;
member functions;
friend return type function_name(Arguments);
……………
};
CODE:
#include<iostream>
using namespace std;
class base
{
int num1,num2;
public:
void getdata()
{
cout<<"\nEnter 1st number: ";
cin>>num1;
cout<<"\nEnter 2nd number: ";
cin>>num2;
}
friend float mean(base ob);
};
OUTPUT:
DISCUSSIONS:
We declare a friend function of float type with name “mean”.A friend function is a
function that can access the private as well as public members of the class and it
calculates the mean of two numbers num1 and num2 and returns the value to the main
function.
Friend Function Like friend class, a friend function can be given a special grant to
access private and protected members. A friend function can be:
a) A member of another class
b) A global function
When friend function is called neither name of object nor dot operator is used.
Friend function can be declared in any section of the class i.e. public or private or
protected.
EXPERIMENT 7
AIM: Write a program to demonstrate the concept of friend function, pointers to data
members, pointers to member functions and pointers to objects.
THEORY:
A friend function is a special function in C++ which in-spite of not being member
function of a class has privilege to access private and protected data of a class.
SYNTAX:
class A
{
data members;
member functions;
friend return type function_name(A&);
};
//pointer to an object
class_name*pointername=newclass_name;
CODE:
#include<iostream>
using namespace std;
class A
{
int x,y;
public:
friend int add(A&);
void getdata(){
cout<<"NUM1: ";
cin>>x;
cout<<"\nNUM2: ";
cin>>y;
}
void showdata(){
cout<<"\nNUM1:"<<x;
cout<<"\tNUM2:"<<y;
}
};
int add(A& a){
int A::*ptrx=&A::x;
int A::*ptry=&A::y;
return a.*ptrx + a.*ptry;
}
int main(){
void(A::*Get)(void)=&A::getdata;
void(A::*Show)(void)=&A::showdata;
A*a1=new A;
(a1->*Get)();
(a1->*Show)();
cout<<"\nSum: "<<add(*a1);
return 0;
}
OUTPUT:
DISCUSSIONS:
We create a class “A” having int data members x and y. A friend function of int type
“add” is declared to add the private data members of class.
In the public section we define getdata( ) and showdata( ) to get the values of x and y
and show them respectively.
Then we use pointers to data members and functions using appropriate syntax and then
we use a pointer to an object a1 to point it to the sum value of x and y.
➢ A friend function is able to access members without the need of inheriting the
class.
➢ Friend function acts as a bridge between two classes by accessing their private
data.
➢ Friends should be used only for limited purpose. If too many functions or
external classes are declared as friends of a class with protected or private data,
it lessens the value of encapsulation of separate classes in object-oriented
programming.
➢ Friend functions have access to private members of a class from outside the
class which violates the law of the data hiding
A pointer to a member of a class differs from a normal pointer: it has both type
information for the type of the member and for the class to which the member belongs.
EXPERIMENT 8
THEORY:
Inheritance is a property wherein an object of one class possesses the properties of
another class and can further inherit the properties to other classes.
Derivation of a class from another derived class is called Multilevel Inheritance.
Class A is the base class for the derived class B, which in turn serves as a base class for
the derived class C. Class B provides a link for the inheritance between A and C
and is known as an intermediate base class
This kind of inheritance can have more than one parent class and can be extended to
any level of inheritance depending upon the requirement of the object of the class.
SYNTAX:
class A
{
... .. ...
};
class B:public A
{
... .. ...
};
class C: public B
{
... ... ...
};
CODE:
#include<iostream>
using namespace std;
class student //base class
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student::get_number(int a)
{
roll_number = a;
}
void student::put_number()
{
cout<<"RollNumber: "<<roll_number<<endl;
}
DISCUSSIONS:
A base class “student” is created followed by its class name and the base class will
have its own properties and methods which will further get inherited by the next
derived class “test” with the access modifier and some scope with it such as private or
public. Finally, the child class “result” will have all the basic and required properties
inherited by the parent class and both the base class and derived class are acting as a
parent class.
THEORY:
Inheritance is a property wherein an object of one class possesses the properties of
another class and can further inherit the properties to other classes.
Hybrid Inheritance is implemented by combining more than one type of
inheritance.
For example: Combining Single Inheritance and Multiple Inheritance.
SYNTAX:
class A
{
………..
};
class B : public A
{
………
};
class C
{
.……..
};
class D : class B, class C
{
………
};
CODE:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
cout << "Enter the value of 'a' : ";
cin>>a;
}
};
class B : public A //Single Inheritance
{
protected:
int b;
public:
void get_b()
{
cout << "Enter the value of 'b' : ";
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
cout << "Enter the value of 'c' : ";
cin>>c;
}
};
class D : public B, public C //Multiple Inheritance
{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
cout << "Multiplication of a,b,c is : " <<a*b*c;
}
};
int main()
{
D d;
d.mul();
return 0;
}
OUTPUT:
DISCUSSIONS:
We create a class A which is the base class and its properties are inherited by a derived
class B which is an example of simple inheritance, in which one class is derived from
a single class which is its base. Now a class C is created. We create a class D which
inherits the properties of both class B and C leading to multiple inheritance, in
which a class is derived from two classes, where one of the parents is also a derived
class.
Combining these two inheritances leads to Hybrid Inheritance.
Hybrid inheritance is explained along with its syntax and example code. Hybrid
inheritance inherits the property of different classes by a combination of different types
of inheritance.
This type of inheritance is basically used when we have a situation where we want to
inherit the property with more than one type of inheritance.