0% found this document useful (0 votes)
10 views

OOPS lab file 178

oops file of dtu

Uploaded by

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

OOPS lab file 178

oops file of dtu

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE AND

ENGINEERING
OBJECT ORIENTED PROGRAMMING

CO 203
LAB FILE

SUBMITTED TO: SUBMITTED BY:

Dr. INDU SINGH HARSH KUMAR


Department of CSE (2K21/CO/178)
INDEX
S. No. Objective Date Sign
EXPERIMENT 1

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);

double add(double x, double y);


double add( int p, double q);
double add(double p, int q);

CODE:
#include<iostream>
#include<cmath>
using namespace std;

int area(int length,int breadth);


float area(float radius);
float area(int a,int b,int c);

int area(int length, int breadth){


return length*breadth;
}
float area(float radius){
return 3.14*radius*radius;
}
float area(int a,int b,int c){
float s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
int main(){
cout<<"Ar. of rectangle: "<<area(6,5)<<endl;
cout<<"Ar.of circle: "<<area(3.5)<<endl;
cout<<"Ar. of triangle: "<<area(3,4,5)<<endl;
return 0;
}

OUTPUT:

DISCUSSIONS:

At first we declare a function of int type with name “area” and it takes 2 arguments
namely length and breadth.

In order to perform function overloading, we declare another function of float


type having the same name. It takes one argument of float type which is the radius.

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.

LEARNINGS AND FINDINGS:


Function overloading means one function can perform many tasks. In C++, a single
function is used to perform many tasks with the same name and different types of
arguments.

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

AIM: Write a program to perform different operations like addition, subtraction,


multiplication, division using inline functions.

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);
}
};

inline void program::sum(){


cout<<"\nSum of numbers: "<<(x+y);
}
inline void program::diff(){
cout<<"\nDifference of numbers: "<<(x-y);
}
inline void program::multiply(){
cout<<"\nMultiplication of numbers: "<<(x*y);
}
int main(){
program p;
p.getdata(40,15);
p.sum();
p.diff();
p.multiply();
p.div();
return 0;
}

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.

LEARNINGS AND FINDINGS:


In case of inline functions, the compiler does not go through the process of switching between the
stack and calling function. Instead, it copies the definition of the inline function and the calling
instruction with that definition.
Inline functions that are small have higher efficiency and better results than the lengthier inline
functions.
Always try to define large functions outside the class, since functions defined inside a class are
automatically defined as inline and this will affect the program negatively. We use scope resolution
(::) for this purpose.
EXPERIMENT 3

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:

• To initialize data members


• To display data members

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.

An Object is an instance of a Class. When a class is defined, only the specification


for the object is defined; no memory or storage is allocated. To use the data and
access functions defined in the class, you need to create objects.

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.

Input: It is used to get data to member variables declared in class.

Display: It is used to display all the data members, i.e the account details.

LEARNINGS AND FINDINGS:

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.

• It is considered good practice to declare the class attributes as private.


Encapsulation ensures better control of our data, because we (or others) can
change one part of the code without affecting other parts
• Increased security of data
EXPERIMENT 4

AIM: Write a program to implement Static Member functions.

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.

LEARNINGS AND FINDINGS:

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;

for(i = 0; i < n; i++)


emp[i].getdata();

cout << "Employee Data - " << endl;

for(i = 0; i < n; i++)


emp[i].display();
return 0;
}

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.

LEARNINGS AND FINDINGS:

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.

• Two for loops are being used-


First one to take the input from user by calling emp[i].getdata() function.
Second one to print the data of Employee by calling the function
emp[i].putdata() function.
EXPERIMENT 6

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);
};

float mean(base ob)


{
return float(ob.num1+ob.num2)/2;
}
int main()
{
base number;
number.getdata();
cout<<"\nMean : "<<mean(number);
return 0;
}

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.

LEARNINGS AND FINDINGS:

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.

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.

A pointer is a type of variable that carries location information. In pointer to object,


the variable will store the address of an class object that we want to interact
with. We initialize the pointer variable by using the C++ new operator to construct a
new object of type class.

Pointers to data members allow you to refer to non-static members of class


objects.

SYNTAX:

class A
{
data members;
member functions;
friend return type function_name(A&);
};

//pointer to data members


return type class_name::*pointername=&class_name::variablename;

//pointer to member functions


void(class_name::*pointername)(argument list)=&class_name::functionname;

//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.

LEARNINGS AND FINDINGS:

➢ 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

Pointers to members allow you to refer to non-static members of class objects. We


cannot use a pointer to member to point to a static class member because the address of
a static member is not associated with any particular object.

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

AIM: Write a program for implementing Multilevel Inheritance.

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;
}

class test : public student //derived class


{
protected:
float sub1;
float sub2;
public:
void get_marks(float, float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
sub1 = x;
sub2 = y;
}
void test :: put_marks()
{
cout<<"Marks in SUB1: "<< sub1 <<endl;
cout<<"Marks in SUB2: "<< sub2 <<endl;
}

class result : public test //derived from derived class


{
float total;
public:
void display(void);
};
void result :: display(void)
{
total = sub1 + sub2;
put_number();
put_marks();
cout << "Total Marks: "<<total<<endl;
}
int main()
{
result student1;
student1.get_number(139);
student1.get_marks(75.0, 81.5);
student1.display();
return 0;
}
OUTPUT:

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.

LEARNINGS AND FINDINGS:

➢ Unlike other inheritance multi-level inheritance has a special feature that it


can be extended to any level of inheritance besides a condition that it depends
on the requirements of the object and the class. Also, a base class can have
more than one parent class. This situation can even arise in the real world
which can be overcome using multi-level inheritance.
➢ Based on the access modifier or visibility the scope is caught and the properties
of the base class get inherited. Access modifiers can be anything from private,
public and protected.
EXPERIMENT 9

AIM: Write a program for implementing Hybrid Inheritance.

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.

LEARNINGS AND FINDINGS:

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy