OOPC Practical Sets 1, 2, 3, 4,5 and 6
OOPC Practical Sets 1, 2, 3, 4,5 and 6
OOPC Practical Sets 1, 2, 3, 4,5 and 6
1) AIM: Write a program to create 4 functions namely add1( ), add2( ), add3( ), add4( )
to demonstrate the concept of all types of function.
Practical Code:
#include<stdio.h>
void add1();
void add2(int,int);
int add3(int,int);
int add4();
int a,b,c,d,sum;
main()
add1();
scanf("%d %d",&a,&b);
add2(a,b);
scanf("%d %d",&a,&b);
int c=add3(a,b);
printf("\n\nAdition 3 is %d",c);
int d=add4();
printf("\n\nAddition 4 is %d",d);
}
void add1()
scanf("%d %d",&a,&b);
sum=a+b;
sum=x+y;
sum=d+e;
return sum;
int add4()
{
printf("\n\nEnter two values for addition\n");
scanf("%d %d",&a,&b);
sum=a+b;
return sum;
Practical Output:
34
Addition1 is: 7
23
Addition2 is: 5
45
Adition 3 is 9
67
Addition 4 is 13
2) AIM: Write a program to create an employee structure having member’s name and
salary. Get data in employee structure through one function and display data
Practical Code:
#include <stdio.h>
//Function Declaration
void get_emp_data();
void print_emp_data();
//structure declaration
struct employee
char name[30];
float salary;
};
int main()
get_emp_data();
print_emp_data();
void get_emp_data()
gets(emp.name);
void print_emp_data()
Practical Output:
Enter details:
Practical Code:
#include <stdio.h>
struct Person
int age;
float weight;
};
int main()
ptr = &p1; /* ptr variable pointing to the address of the structure variable p1 */
scanf("%d",&p1.age);
scanf("%f",&p1.weight);
return 0;
}
Practical Output:
Person age: 35
#include<stdio.h>
int main()
int a, b;
swap(&a, &b);
return 0;
int temp;
temp = *a;
*a = *b;
*b = temp;
Practical Output:
35
void isPrime(int n)
{
int i, flag = 0;
if (flag==0)
cout<<n<<" is a prime number"<<"\n";
else
cout<<n<<" is not a prime number"<<"\n";
}
int main()
{
int n;
cout<<"Enter the number:";
cin>>n;
isPrime(n);
Practical Output:
6) AIM: Write a program to find out the maximum number in a given array.
Using a member function of class containing an array.
Practical Code:
#include <iostream>
using namespace std;
// create a class
class Array
{
// private data member
private:
int array[5];
// public functions
public:
// putArray() function to get the value of the array
void putArray()
{
int i;
for (i = 0; i<= 4; i++)
{
cout << "array [" << i << "]:";
cin >> array[i];
}
}
// for loop to read the whole array from the second term to the second
for (i = 1; i<= 4; i++)
{
// if the value at the index is greater than the max then the value
// will replace the value at the max
if (array[i] > max)
{
max = array[i];
}
}
int main()
{
// create an object
Array A;
array[0]:3
array[1]:5
array[2]:6
array[3]:8
array[4]:2
Largest Number is : 8
#include<iostream>
using namespace std;
class line
{
public:
inline float mul(float x, float y)
{
return (x * y);
}
int main()
{
line obj;
float val1, val2;
cout << "Enter two values:";
cin >> val1>>val2;
cout << "\nMultiplication value is:" << obj.mul(val1, val2);
cout << "\ndivision value is :" << obj.div(val1,val2);
Practical Output:
int main()
{
double m,ans;
int n;
cout<<"\nEnter m and it's power n :";
cin>>m>>n;
if(m==0)
{
ans=power(n);
cout<<"\nPower of "<<n<<" is "<<ans;
}
else
{
ans=power(m,n);
cout<<"\n Power of "<<m<<" is "<<ans;
}
}
Practical Output:
OUTPUT 1:
Enter m and it's power n :5 4
m to the power n =625
OUTPUT 2:
Enter m and it's power n :0 5
Power of 5 is 25
9) AIM: Write a program that overloads volume functions that return
volume of cube, cuboids and cylinder.
Practical Code:
#include<iostream>
using namespace std;
int volume(int); //cube v=a*a*a
int volume(int,int,int); // cuboid V=l*b*h
float volume(int,float); //cylinder V=3.14*r*h
int main()
{
int a,l,b,h,r;
float h1;
cout<<"Enter side of cube:";
cin>>a;
cout<<"Enter length,breadth and height of cuboid: ";
cin>>l>>b>>h;
cout<<"Enter radius and height of a cylinder:";
cin>>r>>h1;
cout<<"\nVolume of cube is :"<<volume(a);
cout<<"\nVolume of cuboid is :"<<volume(l,b,h);
cout<<"\nVolume of cylinder is :"<<volume(r,h1);
}
int volume(int a)
{
return(a*a*a);
}
int volume(int l ,int b,int h)
{
return(l*b*h);
}
float volume(int r,float h1)
{
return(3.14*r*r*h1);
}
Practical Output:
Enter side of cube:3
Enter length, breadth and height of cuboid: 2 4 4
Enter radius and height of a cylinder:2 2.3
Volume of cube is : 27
Volume of cuboid is : 32
Volume of cylinder is : 28.888
PRACTICAL SET-4 (Concept of Class, Setters and Getters)
10) AIM: Write a program to create a class for book having title, price and
publisher having 2 member functions getdetails( ) and setdetails( ). Also
create the object of 2 different books, use getter and setter for both the
objects.
Practical Code:
#include<iostream>
using namespace std;
class book
{
string title;
float price;
string publisher;
public:
void getdetails()
{
cout<<"Enter the Book Title,price and publisher details \n ";
cin>>title>>price>>publisher;
}
void setdetails()
{
cout<<"The book details are as : title,price and publisher \n";
cout<<"title:"<<title<<"\n";
cout<<"price:"<<price<<"\n";;
cout<<"publisher:"<<publisher<<"\n";;
}
};
int main()
{
book b1,b2;
cout<<"Book 1 Details are"<<"\n\n";
b1.getdetails();
b1.setdetails();
cout<<"Book 2 Details are"<<"\n";
b2.getdetails();
b2.setdetails();
return 0;
}
Practical Output:
#include<iostream>
using namespace std;
class book
{
string title;
float price;
string publisher;
public:
void getdetails()
{
cout<<"Enter the Book Title,price and publisher details \n ";
cin>>title>>price>>publisher;
}
void setdetails()
{
cout<<"The book details are as : title,price and publisher \n";
cout<<"title:"<<title<<"\n";
cout<<"price:"<<price<<"\n";;
cout<<"publisher:"<<publisher<<"\n";;
}
};
int main()
{
book b[5];
for(int i=0;i<5;i++)
{
cout<<"Book " <<i<<" get details "<<"\n";
b[i].getdetails();
}
for(int i=1;i<=5;i++)
{
cout<<"Book" <<i<<" Details are"<<"\n";
b[i].setdetails();
}
return 0;
}
Practical Output:
// enter details
void getDetails()
{
cout<<"Enter the title of the book\n ";
cin>>bookTitle;
cout<<"Enter the publication of the book\n ";
cin>>publication;
cout<<"Enter the book's price\n ";
cin>>price;
sr_no++; //static variable automatically by default initialization is 0
ant after sr_no++ it will be 1
no_of_books++;
}
Book b1,b2;
//cout<<"Enter the details for the books\n";
b1.getDetails();
b1.putDetails();
b2.getDetails();
b2.putDetails();
b1.getmaxprice(b1,b2);
getcount(b1);
return 0;
}
Practical Output:
Enter the title of the book
BALAGURURSAMY
Enter the publication of the book
PHI
Enter the book's price
750.50
Sr no : 1
Book Title: BALAGURUSAMY
Book Publication: PHI
Book Price: 750.5
Sr no : 2
Book Title: OOPC
Book Publication: PEARSON
Book Price: 650.5
class A
{
public:
// Only declaration
void fun();
};
int main()
{
A a;
a.fun();
return 0;
}
Practical Output:
fun() called
15) Write a program to create a class for defining COMPLEX numbers and
overload three set functions (setters). The first set function which takes no
argument is used to create objects which are not initialized, second which
takes one argument is used to initialize real and imaginary parts to equal
values and third which takes two argument is used to initialized real and
imaginary to two different values. Define a display function that prints the
complex number.
#include<iostream>
using namespace std;
class complex
{
float real,img;
public:
16) Write a program to create a class TEST with one int member. Define
constructor, destructor and getter for the same. Define a function (outside
class) find_square that takes object as an argument and returns square of
int member of that object. Also define destructor.
Practical Code:
#include <iostream>
using namespace std;
class Test
{
int no;
public:
// Parameterized Constructor (setter method)
Test(int s)
{
cout<<"\n\n****** Inside the Constructor ******* \n\n";
no = s;
}
// Getter
int getNum()
{
return no;
}
void sq();
void Test::sq()
{
int a=no*no;
cout<<"\nSquare of a no= "<<a;
}
int main()
{
//Test myObj;
int n;
cout<<"Enter the no:";
cin>>n;
Test myObj(n);
//myObj.setNum(n);
cout << "Entered no is:"<<myObj.getNum();
myObj.sq();
return 0;
}
Practical Output
Enter the no:5
Entered no is:5
Square of a no= 25
Practical Output:
Enter a single value for real and imaginary parts of first complex number: 2
Enter different values for real and imaginary parts of second complex number: 3
4
Storing the result of addition of first and second complex number into third...
18) Write a program to create a class TIME with members hours, minutes,
and seconds. Read values from keyboard and add two TIME objects by
passing objects to function and display result. Also define destructor.
Practical Code
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
int seconds;
public:
void getTime(void);
void putTime(void);
void addTime(Time T1, Time T2);
};
void Time::getTime(void)
{
cout << "Enter time:" << endl;
cout << "Hours? ";
cin >> hours;
cout << "Minutes? ";
cin >> minutes;
cout << "Seconds? ";
cin >> seconds;
}
void Time::putTime(void)
{
cout << endl;
cout << "Time after add: ";
cout << hours << ":" << minutes << ":" << seconds << endl;
}
Practical Output
Enter time:
Hours? 2
Minutes? 40
Seconds? 20
Enter time:
Hours? 4
Minutes? 50
Seconds? 40
Create a base class called SHAPE. Use this class to store two double type values. Derive two specific
classes called TRIANGLE and RECTANGLE from the base class. Add to the base class, a member
function getdata to initialize base class data members and another member function display to
compute and display the area of figures. Make display a virtual function and redefine this function in
the derived classes to suit their requirements. Using these three classes design a program that will
accept driven of a TRINGLE or RECTANGLE interactively and display the area.