Lab Manual Cs6461 - Object Oriented Programming Lab: Valliammai Engineering College SRM Nagar, Kattankulathur
Lab Manual Cs6461 - Object Oriented Programming Lab: Valliammai Engineering College SRM Nagar, Kattankulathur
Lab Manual Cs6461 - Object Oriented Programming Lab: Valliammai Engineering College SRM Nagar, Kattankulathur
LAB MANUAL
AIM:
To write a C++ program to find the sum for the given variables using function with default
arguments.
ALGORITHM:
1) Start the program.
2) Declare the variables and functions.
3) Give the values for two arguments in the function declaration itself.
4) Call function sum() with three values such that it takes one default arguments.
5) Call function sum() with two values such that it takes two default arguments.
6) Call function sum() with one values such that it takes three default arguments
7) Inside the function sum(), calculate the total.
8) Return the value to the main() function.
9) Display the result.
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
float sum(float a,int b=10,int c=15,int d=20);
int a=2,b=3,c=4,d=5;
clrscr();
cout<<"\nsum="<<sum(0);
cout<<"\nsum="<<sum(a,b,c,d);
cout<<"\nsum="<<sum(a,b,c);
cout<<"\nsum="<<sum(a,b);
cout<<"\nsum="<<sum(a);
cout<<"\nsum="<<sum(b,c,d);
getch();
}
float sum(float i, int j, int k, int l)
{
return(i+j+k+l);
}
Output:
sum=45
sum=14
sum=29
sum=40
sum=47
sum=32
RESULT:
Thus, the given program for function with default arguments has been written and executed
successfully.
Aim:
To implement function with default arguments.
Algorithm:
1. Declare the default function.
2. Invoke the default function.
3. Display the result
Source Code:
#include<iostream.h>
void printLine(char =’_’,int =70);
void main()
{
printLine();
printLine(‘/’);
printLine(‘*’,40);
printLine(‘R’,55);
}
void printLine(char ch, int Repeatcount)
{
int i;
cout<<endl;
for(i=0;i<Repeatcount;i++)
cout<<ch;
}
Output:
-----------------------------------
///////////////////////////////////////
****************************
RRRRRRRRRRRRRRRRRRRRRRR
RESULT:
Thus, the given program for function with default arguments has been written and executed
successfully
Algorithm:
Program:
#include<iostream.h>
Class product
{
int pro_code[50];
float pro_price[50];
int count;
public:
void cnt()
{count=0;}
Void getproduct();
Void display();
Void displaysum();
Void displayproducts();
};
Void product::getproduct()
{
cout<<”Enter product Code:”;
cin>>pro_code[count];
cout<<”Enter product Cost:”;
cin>>pro_price[count];
count++;
}
Void product::displaysum()
{
float sum=0;
for(int i=0;i<count;i++)
sum=sum+pro_price[i];
cout<<”Total Value:”<<sum<<”\n”;
}
Void product::displayproducts()
{
Cout<<” \nCode Price\n”;
for(int i=0;i<count;i++)
{
cout<<”\n”<<pro_code[i];
cout<<” “<<pro_price[i];
}
Cout<<”\n”;
}
int main()
{
product obj;
obj.cnt();
int x;
do
{
Cout<<”Enter choice\n”;
Cout<<”\n1.Add a product”;
Cout<<”\n2.Display a product total value”;
Cout<<”\n3.Display all products”;
Cout<<”\n4.Quit”;
Cin>>x;
switch(x)
{
case1:order.getproduct();
case2:order.displaysum();
case3:order.displayproducts();
case4:break;
default:cout<<”|n Invalid choice”;
}
}while(x!=5);
return 0;
}
RESULT:
Thus the C++ program for implementing arrays as data members was created, executed and
output was verified successfully.
int main()
{
data d,*dp;
dp=&d; //pointer to object
int data::*ptr=&data::a; //pointer to data member
d.*ptr=10;
d.print();
dp->*ptr=20;
dp->print();
}
RESULT:
Thus the C++ program for implementing classes with pointers as data members was created,
executed and output was verified successfully.
}
RESULT:
Thus the C++ program for implementing classes with constant data members was created,
executed and output was verified successfully.
Ex.No:2E CLASSES WITH STATIC MEMBER FUNCTION
Aim:
To implement static member function in class.
Algorithm:
1. Create class test with static data member as count.
2. Create a member function to increment the count.
3. Declare the static data member using scope resolution operator.
4. Display the count value.
Source Code:
#include<iostream.h>
class test
{
int code;
static int count;
public:
void setcode()
{
cout<<”Object Number:”<<code<<”\n”;
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcount();
t2.setcount();
test::showcount();
test t3;
t1.showcode();
t2.showcode();
t3.showcode();
return(0);
}
Output:
count 2
count 3
Object Number 1
Object Number 2
Object Number 3
RESULT:
Thus to write a c++ program and to implement the concept of Static Data member function was
successfully completed.
OUTPUT:
S = 10 -20 30
S = -10 20 -30
RESULT:
Thus to write a c++ program and to implement the concept of unary operator overloading was
successfully completed.
Aim :
To write a C++ program to implement the concept of Binary operator overloading.
Algorithm:
Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator +() to add two complex numbers.
Step 6: Define the function operator –()to subtract two complex numbers.
Step 7: Define the display function.
Step 8: Declare the class objects obj1,obj2 and result.
Step 9: Call the function getvalue using obj1 and obj2
Step 10: Calculate the value for the object result by calling the function operator + and
operator -.
Step 11: Call the display function using obj1 and obj2 and result.
Step 12: Return the values.
Step 13: Stop the program
Source Code:
#include<iostream.h>
#include<conio.h>
class complex
{
float x;
float y;
public:
complex(){}
complex(float real,float imag)
{
x=real;y=imag;
}
complex operator+(complex c);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<"\n";
}
void main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"c3=";
c3.display();
getch();
}
OUTPUT:
C1 = 2.5+j3.5
C2 = 1.6+j2.7
C3 = 4.1+j6.2
RESULT:
Thus to write a c++ program and to implement the concept of binary operator overloading was
successfully completed.
Aim :
To write a C++ program to implement the concept of Function Overloading
Algorithm:
1. Start the program.
2. Create the class with variables and functions.
3. In the main(),declare the variables.
4. Use Volume() function to find volume of cylinder, cube and rectangle.
5. Define the volume() function with necessary arguments for calculation.
6. The values that are passed inside the function call will be matched with the definition
part and appropriate calculations are done.
7. All the three volumes are displayed accordingly by display() function.
8. stop the program.
Source Code:
#include<iostream.h>
#include<conio.h>
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
return(3.14*r*r*h);
}
long volume(long l,int b,int h)
{
return(l*b*h);
}
void main()
{
clrscr();
cout<<"!!!VOLUME!!!\n";
cout<<volume(10)<<endl;
cout<<volume(10,20)<<endl;
cout<<volume(10,20,30)<<endl;
getch();
}
OUTPUT:
1000
157.26
112500
RESULT:
Thus to write a c++ program and to implement the concept of function overloading was
successfully completed.
Ex.No:4.a INHERITANCE
Aim:
To write a C++ program for implementing the inheritance concept.
Algorithm:
1. Start the process.
2. Define the base class with variables and functions.
3. Define the derived class with variables and functions.
4. Get two values in main function.
5. Define the object for derived class in main function.
6. Access member of derived class and base class using the derived class object.
7. Stop the process.
Program:
#include<iostream.h>
#include<conio.h>
class base
{
public:
int x;
void set_x(int n)
{x=n;
}
void show_x()
{
cout<<”\n\t Base class….”;
cout<<”\n\tx=”<<x;
}
};
class derived:public base
{
int y;
public:
void set_y(int n)
{
y=n;
}
void show_xy()
{
cout<<”\n\n\t derived class…”;
cout<<”\n\tx=”<<x;
cout<<”\n\ty=”<<y;
}
};
void main()
{
derived obj;
int x,y;
clrscr();
cout<<”\n enter the value of x: ”;
cin>>x;
cout<<”\n enter the value of y: ”;
cin>>y;
obj.set_x(x);//inherits base class
obj.set_y(y);//acess member of derived class
obj.show_x();//inherits base class
obj.show_xy();//acess member of derived class
getch();
}
OUTPUT:
enter the value of x 10
enter the value of y 20
base class….
x=10
derived class…..
x=10
y=20
RESULT:
Thus the C++ program for inheritance was created, executed and output was verified
successfully.
Ex No 4B VIRTUAL FUNCTION
Aim:
To write a C++ program to implement the concept of Virtual functions
Algorithm:
1.Start the program.
2.Define a base class called base and define a function called display as virtual in it.
3.Derive a new class called derv1,derv2 using a base class called base and define a function
called display in the respective classes.
4.Declare a base class pointer in main function.
5Declare objects for derv1 and derv2 classes respectively.
6.Assign a derv1 and derv2 obj to base pointer
7.Now display function shows the derv1 and derv2 class’ function respectively.
Source Code:
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void display()
{
cout<<"Base class display is called\n";
}
};
class derv1 : public base
{
public:
void display()
{
cout<<"\nDerv1's display called\n";
}
};
class derv2:public base
{
public:
void display()
{
cout<<"\nDerv2's display called\n";
}
};
void main()
{
clrscr();
base *ptr;
derv1 d1;
derv2 d2;
ptr=&d1;
ptr->display();
ptr=&d2;
ptr->display();
getch();
}
Output:
Derv1's display called
Derv2's display called
RESULT:
Thus the C++ program for virtual function was created, executed and output was verified
successfully
Output:
BEFORE BUBBLE SORTING
12, 11, 15,13,17,14,16,19,18
AFTER BUBBLE SORTING
11, 12, 13,14,15,16,17,18,19
RESULT:
Thus the C++ program for function template was created, executed and output was verified
successfully
EX.NO 5A. FILE HANDLING-SEQUENTIAL ACCESS
Aim:
To implement a file handling concept using sequential access.
Algorithm:
1.Start the process
2.Get the input string
3.Write the input string char by char into a file called “Text” using put() function
4.Read the input string char by char from the file called “Text” using get() function
5.Display the result
Source Code:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
int main()
{
Char str[80];
Cout<<”Enter a string:”;
Cin>>str;
Int len=strlen(str);
Fstream file;
File.open(“TEXT”,ios::in|ios::out);
For(int i=0;i<len;i++)
File.put(str[i]);
File.seekg(0);
Char ch;
While(file)
{
File.get(ch);
Cout<<ch;
}
return 0;
}
RESULT:
Thus the C++ program for file handling- sequential access concept was created, executed and
output was verified successfully
int main()
{
ifstream IFileObject("D:\\ExampleFile.txt");
if (!IFileObject)
{
cout << "File cannot be opened" << endl;
exit(1);
}
string lineread;
Output:
ing to open a file called text
RESULT:
Thus the C++ program for file handling- random access concept was created, executed and
output was verified successfully
EX.NO 6A. SIMPLE JAVA PROGRAM
Aim:
To write a java program to find volume of box.
Algorithm:
PROGRAM:
class Box
{
int width=2,height=2,depth=2,a;
void writedata()
{
System.out.print("volume of box is:");
System.out.println(width*height*depth);
}
}
public class example
{
public static void main(String args[])
{
Box mybox1=new Box();
mybox1.writedata();
}
}
OUTPUT:
c:\jdk1.3\bin>java example
volume of box is 8
RESULT:
Thus a java program is executed for finding volume of box.
EX.NO 7. PACKAGES
Aim:
Import pack.*;
class accountbalance
{
public static void main(String arg[])
{
balance current[]=new balance[3];
current[0]=new balance("K.J.Fielding",123.23);
current[1]=new balance("Will tell",157.02);
current[2]=new balance("Tom jackson",-12.33);
for(int i=0;i<3;i++)
current[i].show();
}
}
OUTPUT:
C:\jdk1.3\bin>java accountbalance
-->Tom jackson:$-12.33
RESULT:
Thus java program for finding the balance amount using packages is verified.
OUTPUT:
C:\jdk1.3\bin>javac interfacetest.java
C:\jdk1.3\bin>java interfacetest
Area of rectangle = 200.0
Area of circle=314.0
RESULT:
Thus a java program is executed for the concept of user defined interface
Aim:
Source Code:
import java io.*;
class Th1 extends Thread
{
public void run()
{
try
{
thread.sleep(1000);
System.out .println(“name:elan”);
System.out println(“age:19”);
}
catch(InterruptedException i)
{
}
}
}
class Th2 extends Thread
{
public void run()
{
try
{
thread.sleep(2000);
System.out .println(“class:b.tech I>T”);
System.out println(“col:vec”);
}
catch(InterruptedException i)
{
}
}
}
class Th3 extends Thread
{
public void run()
{
try
{
thread.sleep(3000);
System.out .println(“place:chennai”);
System.out println(“area:kattankulathur”);
}
catch(InterruptedException i)
{
}
}
}
class thdemo
{
public static void main(String args[ ])
{
Th1 t1=new Th1( );
t1.start( );
Th2 t2=new Th2( );
t2.start( );
Th3 t3=new Th3( );
t3.start( );
}
}
OUTPUT:
Name: elan
Age: 19
Class: b.tech I.T
Col: vec
Place: chennai
Area: kattankulathur
RESULT:
Thus threading using java program was verified and implemented.
OUTPUT
division by zero
y1
RESULT:
Thus pre defined exception using java program was verified and implemented.
OUTPUT
C:\jdk1.5.0\bin>javac JavaException.java
C:\jdk1.5.0\bin>java JavaException
Exception Number 2
RESULT:
Thus user defined exception using java program was verified and implemented.