Department of Electrical and Electronics Engineering: Lab Manual
Department of Electrical and Electronics Engineering: Lab Manual
Department of Electrical and Electronics Engineering: Lab Manual
ENGINEERING
LAB MANUAL
CS6461
OBJECT ORIENTED PROGRAMMING LABORATORY
Ex: No: 1(a) FUNCTION OVERLOADING
Aim:
Algorithm:
Step 3: Invoke the main function in that of the set and value.
Program:
#include
#include
int volume(int);
doublevolume(int);
long volume(long,int,int);
int main()
clrscr();
cout<<volume(10)<<endl;< span=""></volume(10)<<endl;<>
cout<<volume(2,5,8)<<endl;< span=""></volume(2,5,8)<<endl;<>
cout<<volume(100,75,15)<<endl;< span=""></volume(100,75,15)<<endl;<>
getch();
int volume(int s)
{
return(s*s*s);
return(3.14*r*r*h);
return(l*b*h);
Output:
1000
80
112500
Result:
Thus the implementation of c ++ program for function overloading is executed and the output
has been verified.
Ex: No: 1(b) DEFAULT ARGUMENT IN C++
Aim:
Algorithm:
Program:
#include
#include
float si(float=1000.00,int=2,float=0.02);
void main()
clrscr();
float p,r;
int n;
cin>>p;
cin>>n;
getch();
return((pr*no*ra)/pr);
Output:
Result:
Thus the implementation of c ++ program for default argument is executed and the output has
been verified.
Ex: No: 2 SIMPLE CLASS DESIGN IN C++, NAME SPACES, OBJECT CREATION
Aim:
To write a c ++ program to implement the simple interest class design, name space, object
creation
Algorithm:
Step 2: Create the class and declare the variable and functions of the class.
Step 3: In main function, objects are created and the respective functions are called for the
Function.
Step 4: Get n value and calculation function is repeat by using for loop.
Program:
#include
#include
int f1=1,f2=1,f3;
class fib
public:
void calculation()
f3=f2+f1;
cout<<f3<<endl;< span=""></f3<<endl;<>
f1=f2;
f2=f3;
};
void main()
{
clrscr();
int n;
fib obj;
cin>>n;
obj.calculation();
getch();
Output:
13
Result:
Thus the implementation of c ++ program for simple class design c ++, name space, object
creation is executed and the output has been verified.
To write a c ++ program to implement the dynamic memory allocation, destructor, and copy
constructor
Algorithm:
Step 2: Create class string with two constructors the first is an empty constructor, which allows
declaring an array of string, the second constructor initiates the length of the string and allocates
unnecessary space for the string to the stored and create the string itself.
Step 5: Declare the main function using and display the string S1, S2 and display destructor data
and display the concatenate string S3 display, the copy constructed string S4.
Program:
#include
#include
#include
int count=0;
class string
private:
char*str;
public:
string(char*s)
count++;
int len=strlen(s);
str=new char[len+1];
strcpy(str,s);
string()
delete str;
void display()
cout<<str<<endl;< span=""></str<<endl;<>
int len;
len=strlen(s1.str)+strlen(s2.str);
str=new char[len+1];
strcpy(str,s1.str);
strcat(str," ");
strcat(str,s2.str);
~string()
count--;
}
};
int main()
clrscr();
string s1="SUPER";
string s2="STAR";
string s3;
string s4(s1);
s1.display();
s2.display();
s3.concat(s1,s2);
s3.display();
s4.display();
getch();
return(0);
Output:
SUPER
STAR
SUPER STAR
STAR
Result:
Thus the implementation of c ++ program for the dynamic memory allocation, destructor and
copy constructor is executed and the output has been verified.
Ex: No: 4 OPERAROR OVERLOADING, FRIEND FUNCTION.
Aim:
Algorithm:
Step 2: Create a class complex and declare real and imaginary part value and get the value.
Step 5: Declare the main function and collect the all data and display it.
Program:
#include
#include
class complex
private:
float real;
float img;
public:
complex()
complex(int realpart)
real=realpart;
}
void realpart()
cout<<"realpart:";
cin>>real;
cout<<"imaginarypart:";
cin>>img;
oid outdata()
cout<<"("<<real;< span=""></real;<>
cout<<"+i"<<img<<")";< span=""></img<<")";<>
};
complex c;
c.real=c1.real+c2.real;
c.img=c1.img+c2.img;
return(c);
}
complex operator-(complex c1,complex c2)
complex temp;
temp.real=c1.real-c2.real;
temp.img=c1.img-c2.img;
return(temp);
complex temp;
float qt;
qt=(c2.real*c2.real+c2.img*c2.img);
temp.real=(c1.real*c2.real+c2.img*c2.img)/qt;
temp.img=(c1.img*c2.real-c1.real*c2.img)/qt;
return(temp);
complex temp;
temp.real=(c1.real*c2.real)-(c1.img*c2.img);
temp.img=(c1.real*c2.img)-(c1.img*c2.real);
return(temp);
void main()
{
clrscr();
complex c1,c2,c3;
c1.realpart();
c2.realpart();
c3=c1+c2;
cout<<"addition:";
c3.outdata();
c3=c1-c2;
cout<<endl<<"subtraction:";< span=""></endl<<"subtraction:";<>
c3.outdata();
c3=c1*c2;
cout<<endl<<"multiplication:";< span=""></endl<<"multiplication:";<>
c3.outdata();
c3=c1/c2;
cout<<endl<<"division:";< span=""></endl<<"division:";<>
c3.outdata();
getch();
Output:
Real Part: 1
Imaginary Part: 2
Real Part: 1
Imaginary Part: 2
Addition: (2+i0)
Subtraction: (0+i0)
Multiplication: (-3+i0)
Division: (1+i0)
Result:
Thus the implementation of c ++ program for the operator overloading with friend function is
executed and the output has been verified.
Algorithm:
Step 5: Declare the main function and collect the all data and display it.
Program:
#include
#include
struct A {
return *this;
A& operator=(A&) {
};
class B {
A a;
};
struct C {
C& operator=(C&) {
return *this;
C() { }
};
void main()
clrscr();
B x, y;
x = y;
A w, z;
w = z;
C i;
const C j();
// i = j;
getch();
Output:
A::operator (A&)
Result:
Thus the implementation of c ++ program for overloading assignment operator is executed and
the output has been verified.
Algorithm:
Step 2: Declare the main function and get the float value.
Program:
#include
#include
void main()
clrscr();
float num=98.76;
int x1=(int)num;
int x2=int(num);
int x3=static_cast(num);
cout<<x1=<<x1<<endl;< span=""></x1<<endl;<>
cout<<x2=<<x2<<endl;< span=""></x2<<endl;<>
cout<<x3=<<x3<<endl;< span=""></x3<<endl;<>
getch();
Output:
X1=98
X2=98
X3=98
Result:
Thus the implementation of c ++ program for type conversion is executed and the output has
been verified.
Ex: No: 6 INHERITANCES, RUNTIME POLYMORPHISM
Aim:
Algorithm:
Step 2: Create a class account with the following member variable name, acc_no, acc_type, and
balance.
(i).For current account information like balance gets deposit and get withdrawal amount.
(ii).For saving account information like balance get deposit and get withdrawal amount.
Step 4: Write a member function to get the deposit and withdrawal amount and to update the
balance information for current and savings etc.
Step 5: Write a member function to display the balance information for respective account.
Program:
#include
#include
class account
char name[25];
char acc_no[20];
char acc_type[15];
float balance;
public:void readdata()
cout<<"enter name:";
cin>>name;
cout<<"account number:";
cin>>acc_no;
cout<<"account type:";
cin>>acc_type;
cout<<"balance:";
cin>>balance;
void displayaccountdata()
cout<<"name:"<<name<<endl;< span=""></name<<endl;<>
cout<<"balance:"<<balance<<endl;< span=""></balance<<endl;<>
int bal()
return balance;
};
protected:
int deposit,withdraw,balance;
public:
int getdeposit()
cin>>deposit;
return deposit;
int getwithdraw()
cin>>withdraw;
return withdraw;
};
private:
int balance;
public:
void depositbal()
balance=getdeposit()+bal();
void withdrawbal()
{
balance=bal()-getwithdraw();
};
protected:
int deposit,withdraw,balance;
public:int getdeposit()
cin>>deposit;
return deposit;
int getwithdraw()
cin>>deposit;
return deposit;
};
private:
int balance;
public:
void depositbal()
balance=getdeposit()+bal();
void withdraw()
};
void main()
clrscr();
current c1;
int option,choice;
c1.readdata();
cin>>option;
if(option==1)
cin>>choice;
if(choice==2)
{
c1.displayaccountdata();
c1.depositbal();
else if(choice==1)
c1.displayaccountdata();
c1.withdrawbal();
if(option==2)
cin>>choice;
c1.displayaccountdata();
c1.depositbal();
if(choice==1)
c1.displayaccountdata();
c1.withdrawbal();
getch();
}
Output:
Account type: AB
Balance: 10000
1. Withdraw 2.Deposit
Name: VIJAY
Account Type: AB
Balance: 10000
Result:
Aim:
To write a Program for Exception Handling Divide by zero Using C++ Programming
Algorithm:
Program:
#include
#include
void main()
int a,b,c;
float d;
clrscr();
cin>>a;
cin>>b;
try
if((a-b)!=0)
d=c/(a-b);
else
throw(a-b);
catch(int i)
getch();
Output:
Result:
Thus the implementation of c ++ program for exceptional handling is executed and the output
has been verified.
Ex: No: 8 TEMPLATE DESIGN IN C ++
Aim:
Algorithm:
Step 2: Declare the main function and get the swap variables a, b, and c, d.
Step 3: We can invoke the swap () function like carry ordinary function.
Program:
#include
#include
void swap(int,int);
void swap(float,float);
void main()
clrscr();
int a,b;
float c,d;
cin>>a>>b;
cin>>c>>d;
swap(a,b);
swap(c,d);
template
t temp;
temp=x;
x=y;
y=temp;
cout<<x<<y;< span=""></x<<y;<>
getch();
Output:
13
65
23
78
65137823
Result:
Thus the implementation of c ++ program for template design is executed and the output has
been verified.
Ex: No: 9 PROGRAM DEVELOPMENT USING STL
Aim:
To write the c++ program to implement the program development using STL
Algorithm:
Step 3: We can invoke the stack empty(),push(), pop() function like carry ordinary function.
Program:
#include
#include "Stack.h"
int main(){
Stack myStack;
if (myStack.empty())
else
myStack.push(i);
if (myStack.empty())
else
cout<<"Stack is not empty"<<endl;< span=""></endl;<>
myStack.writeStack();
cout<<endl;< span=""></endl;<>
while (!myStack.empty())
myStack.pop();
if (myStack.empty())
else
return 0;
Output:
Stack is empty
the top of the Stack is: 9
Stack is not empty
9876543210
My Stack size is: 10
Stack is empty
top (): Stack is empty
Result:
Thus the implementation of c ++ program for development of STL is executed and the output
has been verified.
Ex: No: 10(a) SIMPLE CLASS DESIGN IN JAVA
Aim:
Algorithm:
Step 4: For i value from 1 to 10 repeat the process by using for loop.
Program:
import java.io.*;
class simple
s=s+i;
System.out.println("sum of n numbers:"+s);
Output:
Sum of n number: 1
Sum of n number: 3
Sum of n number: 6
Sum of n number: 10
Sum of n number: 15
Sum of n number: 21
Sum of n number: 28
Sum of n number: 36
Sum of n number: 45
Sum of n number: 55
Result:
Thus the implementation of JAVA program for the sum of n numbers is executed and the output
has been verified.
Ex: No: 10(b) FIBONACCI SERIES
Aim:
Algorithm:
Program:
import java.io.*;
classfibo
f=a+b;
System.out.println(f);
a=b;
b=f;
Output:
13
21
34
Result:
Thus the implementation of JAVA program for the Fibonacci series is executed and the output
has been verified.
Ex: No: 10(c) PRINTING MULTIPLICATION TABLE
Aim:
Algorithm:
Step 2: Create class multi table and declare the variable column and row.
Step 3: For the row value for 1 to 10 repeat the step 4 and 5.
Program:
import java.io.*;
class multitable
System.out.println("Multiplication table\n");
y=row*col;
System.out.print(" "+y);
System.out.println("\n");
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
Result:
Thus the implementation of JAVA program for printing multiplication table is executed and the
output has been verified.
Ex: No: 11 DESIGNING PACKAGE WITH JAVA PACKAGE EEE
Aim:
To write a JAVA programs to implement designing package with JAVA package EEE
Algorithms:
Step 2: Create a package called name which contains the class addition with member function to
perform addition operation.
Step 4: From the main class make a function call to the package.
Programs:
package eee;
import java.io.*;
int a,b,c;
c=a+b;
System.out.println("sum is"+c);
import java.io.*;
//import eee.addition;
class sum
d1.add(80,20);
Output:
Sum is 100
Result:
Thus the implementation of JAVA program for Designing package with JAVA package EEE is
executed and the output has been verified.
Ex: No: 12 INTERFACES AND INHERITANCE IN JAVA
Aim:
Algorithm:
Step 2: Create and interface named sports which contains member function.
Step 5: With respect to create object make a function call to the function on other classes
including interface.
Programs:
//Interface program
interface Area
//Inheritance program
return(x * y);
}
class Triangle implements Area
return(x * y/2);
class InterfaceArea
Area area;
area = rect;
area = tri;
Output:
Area of a rectangle=2.0
Area of a triangle=10.0
Result:
Thus the implementation of JAVA program for interface and inheritance is executed and the
output has been verified.
Ex: No: 13 EXCEPTIONS HANDLING IN JAVA
Aim:
Algorithm:
Program:
import java.io.*;
try
catch(ArrayIndexOutOfBoundsException e)
System.out.println("Exception thrown:"+e);
}
Output:
Result:
Thus the implementation of JAVA program for exception handling is executed and the output
has been verified.
Ex: No: 14 JAVA I/O
Aim:
Algorithm:
import java.io.*;
class Echo
String inData;
inData = stdin.readLine();
Output:
Result:
Thus the implementation of JAVA program for Java I/O program is executed and the output has
been verified.
Ex: No: 15 DESIGN MULTITHREAD PROGRAM IN JAVA
Aim:
Algorithm:
Thread t=Thread.currentThread();
System.out.println("current thread:"+t);
t.setName("my thread");
try
for(int n=5;n>0;n--)
System.out.println(n);
Thread.sleep(1000);
catch(InterruptedException ie)
Output:
Result:
Thus the implementation of JAVA program for design multithread program is executed and the
output has been verified.