Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

OOPC Practical Sets 1, 2, 3, 4,5 and 6

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 36

PRACTICAL SET-1 (Basics of C)

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

//Function call with no arguments and no return values

add1();

//Function call with arguments but no return values

printf("\n\nEnter two values for addition\n");

scanf("%d %d",&a,&b);

add2(a,b);

//Function call with arguments and return values

printf("\n\nEnter two values for addition\n");

scanf("%d %d",&a,&b);

int c=add3(a,b);

printf("\nAnswer for Function with ARGUMENTS and RETURN VALUES\n");

printf("\n\nAdition 3 is %d",c);

int d=add4();

printf("\nAnswer for Function with NO ARGUMENTS but RETURN VALUES\


n");

printf("\n\nAddition 4 is %d",d);
}

//Function with NO ARGUMENTS amd NO RETURN VALUES

void add1()

printf("Enter two values for addition\n");

scanf("%d %d",&a,&b);

sum=a+b;

printf("\nAnswer for Function with NO ARGUMENTS and NO RETURN VALUES\n");

printf("\nAddition1 is: %d",sum);

//Function with ARGUMENTS but NO RETURN VALUES

void add2(int x,int y)

sum=x+y;

printf("\nAnswer for Function with ARGUMENTS but NO RETURN VALUES\n");

printf("\n\nAddition2 is: %d",sum);

//Function with ARGUMENTS and RETURN VALUES

int add3(int d,int e)

sum=d+e;

return sum;

//Function with NO ARGUMENTS but RETURN VALUES

int add4()

{
printf("\n\nEnter two values for addition\n");

scanf("%d %d",&a,&b);

sum=a+b;

return sum;

Practical Output:

Enter two values for addition

34

Answer for Function with NO ARGUMENTS and NO RETURN VALUES

Addition1 is: 7

Enter two values for addition

23

Answer for Function with ARGUMENTS but NO RETURN VALUES

Addition2 is: 5

Enter two values for addition

45

Answer for Function with ARGUMENTS and RETURN VALUES

Adition 3 is 9

Enter two values for addition

67

Answer for Function with NO ARGUMENTS but RETURN VALUES

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

using another function. Use concept of structure and function.

Practical Code:

#include <stdio.h>

//Function Declaration

void get_emp_data();

void print_emp_data();

//structure declaration

struct employee

char name[30];

float salary;

};

struct employee emp; //Structure variable

int main()

get_emp_data();

print_emp_data();

/*read employee details*/

void get_emp_data()

printf("\nEnter details :\n");

printf("\nEnter Employee Name :");

gets(emp.name);

printf("\nEnter Employee Salary:");


scanf("%f",&emp.salary);

/*print employee details*/

void print_emp_data()

printf("\n\nEntered details are:");

printf("\nEmployee Name: %s",emp.name);

printf("\nEmployee Salary: %f\n",emp.salary);

Practical Output:

Enter details:

Enter Employee Name: Ramesh Sharma

Enter Employee Salary:35000

Entered details are:

Employee Name: Ramesh Sharma

Employee Salary: 35000.000000


3) AIM: Write a program to create a person structure having age and weight
as structure members. Get the data and print the data using pointers. Use
concept of pointers with structure.

Practical Code:

#include <stdio.h>

// create a structure Person using the struct keyword

struct Person

// declare the members of the structure

int age;

float weight;

};

int main()

struct Person p1; // declare the Person variable

struct Person *ptr; // create a pointer variable (*ptr)

ptr = &p1; /* ptr variable pointing to the address of the structure variable p1 */

printf("\nEnter the age of the person : ");

scanf("%d",&p1.age);

printf("\nEnter the weight of the person :");

scanf("%f",&p1.weight);

// print the details of the Person

printf("Details of the person are : \n");

printf ("Person age: %d\t", (*ptr).age);

printf ("\nPerson Weight: %f\t", (*ptr).weight);

return 0;
}

Practical Output:

Enter the age of the person: 35

Enter the weight of the person :78

Details of the person are:

Person age: 35

Person Weight: 78.000000

4) AIM: Write a program to swap 2 values of a variables by using pointer


Practical Code:

#include<stdio.h>

void swap(int*, int*);

int main()

int a, b;

printf("Enter values for a and b\n");

scanf("%d%d", &a, &b);

printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);

swap(&a, &b);

printf("\nAfter swapping: a = %d and b = %d\n", a, b);

return 0;

void swap(int *a, int *b)

int temp;

temp = *a;

*a = *b;

*b = temp;

Practical Output:

Enter values for a and b

35

Before swapping: a = 3 and b = 5

After swapping: a = 5 and b = 3

PRACTICAL SET-2 (Basics of C++)


5) AIM: Write a program to check whether given number is the prime
number or not, using a member function.
Practical Code:
#include <iostream>

using namespace std;

void isPrime(int n)
{
int i, flag = 0;

for(i=2; i<=n/2; ++i)


{
if(n%i==0)
{
flag=1;
break;
}
}

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:

Enter the number:3


3 is a prime number

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

// largest() function to find the largest number in the array


int largest()
{
// let the first element of the array is max
int i,max;
max = array[0];

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

// returning the largest number


return max;
}
};

int main()
{
// create an object
Array A;

// int type variable to store the largest number in it


int max;

// function is called by the object to store the array


A.putArray();

// largest() function is called by the object to find the


// largest value in the array
max = A.largest();

cout << "Largest Number is : " << max;


return 0;
}
Practical Output:

array[0]:3
array[1]:5
array[2]:6
array[3]:8
array[4]:2
Largest Number is : 8

PRACTICAL SET-3 (Functions, Inline Functions and Function Overloading


in C++)
7) AIM: Write a program to demonstrate the use of inline functions by
creating 2 inline functions. One inline function for multiplication operation
and other inline function for demonstrating division operation.
Practical Code:

#include<iostream>
using namespace std;
class line
{
public:
inline float mul(float x, float y)
{
return (x * y);
}

inline float div(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:

Enter two values:4.5


2.0
Multiplication value is:9
division value is :2.25

8) AIM: Write a function power to raise a number m to power n. The


function takes a double value for m and int value for n. Use default value
for n to make the function to calculate squares when this argument is
omitted.
Practical Code:
#include<iostream>
#include<math.h>
using namespace std;
double power(double m,int n=2)
{
double t;
t=pow(m,n);
return t;
}

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:

/* C++ program to find Volume using Function Overloading */

#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:

Book 1 Details are


Enter the Book Title,price and publisher details
Balagurusamy
450.65
PHP
The book details are as: title,price and publisher
title:Balagurusamy
price:450.65
publisher:PHP

Book 2 Details are


Enter the Book Title,price and publisher details
BALAGURUSAMY
654.80
PHI
The book details are as: title,price and publisher
title:BALAGURUSAMY
price:654.8
publisher:PHI
11) AIM: Write a program to create an array of 5 objects for previous
program.

#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:

Book 1 get details


Enter the Book Title,price and publisher details
Balagurusamy
56.745
PHP
Book 2 get details
Enter the Book Title,price and publisher details
DPC
65.70
PHI
Book 3 get details
Enter the Book Title,price and publisher details
Gurusamy
75.75
Pearson
Book 4 get details
Enter the Book Title,price and publisher details
AAVR
600.35
TH
Book 5 get details
Enter the Book Title,price and publisher details
IOT
650.35
Pearson

Book 1 Details are


The book details are as: title,price and publisher
title:Balagurusamy
price:56.75
publisher:PHP
Book 2 Details are
The book details are as: title,price and publisher
title:DPC
price:65.7
publisher:PHI
Book 3 Details are
The book details are as: title,price and publisher
title:Gurusamy
price:75.75
publisher:Pearson
Book 4 Details are
The book details are as : title,price and publisher
title:AAVR
price:600.35
publisher:TH
Book 5 Details are
The book details are as: title,price and publisher
title:IOT
price:650.35
publisher:Pearson
12) AIM: Write a program to find the maximum price of 2 books and
return that book as object. Use object as function argument.
13) AIM: With reference to previous program, add 2 member variables
serial_no and no_of_books (declare both as static variables) in class named
books. Create a friend function getcount( ) to demonstrate the concept of
friend function.
Practical Code:
#include<iostream>
using namespace std;
class Book
{
private:
string bookTitle, publication;
double price;
static int sr_no;
static int no_of_books;
public:

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

//print details of book


void putDetails()
{
cout<<"\nSr no : "<<sr_no<<endl;
cout<<"Book Title: "<<bookTitle<<endl;
cout<<"Book Publication: "<<publication<<endl;
cout<<"Book Price: "<<price<<endl;
}
// passing agrument as object in class function
void getmaxprice(Book b1, Book b2)
{
if(b1.price > b2.price)
{
cout<<"\nBook "<<b1.bookTitle<< " has highest Price:="<<b1.price;
}
else
{
cout<<endl<<"\nBook "<<b2.bookTitle<< " has highest
Price:="<<b2.price<<endl;
}
}

// friend function returns total no of books entered


friend void getcount(Book b1)
{
cout<<"\n\nTotal number of book Entered="<<no_of_books<<endl<<endl;
}
};
int Book :: sr_no;
int Book :: no_of_books;
int main()
{

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

Enter the title of the book


OOPC
Enter the publication of the book
PEARSON
Enter the book's price
650.50

Sr no : 2
Book Title: OOPC
Book Publication: PEARSON
Book Price: 650.5

Book BALAGURUSAMY has highest Price:=750.5

Total number of books Entered=2


14) AIM: Write a Program to demonstrate the use of scope resolution.
//C++ program to show that scope resolution operator:: is
// used to define a function outside a class
#include <iostream>
using namespace std;

class A
{
public:
// Only declaration
void fun();
};

// Definition outside class using ::


void A::fun()
{
cout << "fun() called";

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:

// function with no argument function


void setComplexno()
{
cout<<"\nEnter value of Real no";
cin>>real;
cout<<"\nEnter value of Imganary no";
cin>>img;
}
//function with one agrgument assigning same value to both real and
img
void setComplexno(float x)
{
real=img=x;
}
//function with two agrgument assigning to both real and img
void setComplexno(float x, float y)
{
real=x;
img=y;
}
//print complex number
void printComplexno()
{
cout<<"\n Complex Number is as below:\n";
cout<<real<<"+j"<<img<<endl<<endl;
}
};
int main()
{
complex c;
// calling no argument function
c.setComplexno();
//calling function to print complex number
c.printComplexno();
float f1,f2;
cout<<"Enter any two float value\n";
cin>>f1>>f2;
// calling one argument function that assign same value(f1) to real and img
number
c.setComplexno(f1);
//calling function to print complex number
c.printComplexno();
// calling two argument function that assign f1= real and f2=img number
c.setComplexno(f1,f2);
//calling function to print complex number
c.printComplexno();
return (0);
}

PRACTICAL SET-5 (Constructor, destructor)

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

//Declaration of the Destructor of the Test Class


~Test()
{
cout << "\n\n****** Inside the Destructor ******* \n\n";
}
};

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

****** Inside the Constructor *******

Entered no is:5
Square of a no= 25

****** Inside the Destructor *******

17) Write a program to perform addition of two complex numbers using


constructor overloading. Define add function outside the class that returns
the addition.
Practical Code:
#include<iostream>
using namespace std;
class complexno
{
int real,imag;
public:
complexno() //Default constructor 1
{
real=0;
imag=0;
}
complexno(int i) //Parameterized constructor with single arguments
{
real=i;
imag=i;
}
complexno(int a,int b)
{
real=a;
imag=b;
}
void add(complexno c1, complexno c2); //Parameterized constructor with two arguments
// {
// real = c1.real+c2.real;
// imag = c1.imag+c2.imag;
// }
void display()
{
cout<<real<<"+"<<imag<<"i";
}
};
void complexno :: add(complexno c1, complexno c2)
{
real = c1.real+c2.real;
imag = c1.imag+c2.imag;
}
int main()
{
cout<<"\n\n Program to perform addition of two complex numbers using constructor
overloading \n";
int real,imag;
cout<<"\n Enter a single value for real and imaginary parts of first complex number : ";
cin>>real;
complexno c1(real);
cout<<"\n First complex number is given by - ";
c1.display();
cout<<"\n\n Enter different values for real and imaginary parts of second complex number :
";
cin>>real>>imag;
complexno c2(real,imag);
cout<<"\n Second complex number is given by - ";
c2.display();
complexno c3;
cout<<"\n\n Initially third complex number is - ";
c3.display();
cout<<"\n\n Storing the result of addition of first and second complex number into third...";
c3.add(c1,c2);
cout<<"\n\n Now third complex number is given by - ";
c3.display();
cout<<"\n";
return 0;
}

Practical Output:

Program to perform addition of two complex numbers using constructor overloading

Enter a single value for real and imaginary parts of first complex number: 2

First complex number is given by - 2+2i

Enter different values for real and imaginary parts of second complex number: 3
4

Second complex number is given by - 3+4i

Initially third complex number is - 0+0i

Storing the result of addition of first and second complex number into third...

Now third complex number is given by - 5+6i

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

void Time::addTime(Time T1, Time T2)


{
this->seconds = T1.seconds + T2.seconds;
this->minutes = T1.minutes + T2.minutes + this->seconds / 60;
;
this->hours = T1.hours + T2.hours + (this->minutes / 60);
this->minutes %= 60;
this->seconds %= 60;
}
int main()
{
Time T1, T2, T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1, T2);
T3.putTime();
return 0;
}

Practical Output
Enter time:
Hours? 2
Minutes? 40
Seconds? 20
Enter time:
Hours? 4
Minutes? 50
Seconds? 40

Time after add: 7:31:0

PRACTICAL SET-6 (Operator Overloading)

19) Implement a program to demonstrate the concept of operator


overloading. Overload ‘+’ and ‘=’ operators.
Practical Code
#include <iostream>
using namespace std;
class Distance
{
private:
int feet; // 0 to infinite
int inches; // 0 to 12
public:
// required constructors
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches = i;
}
void operator = (const Distance &D )
{
feet = D.feet;
inches = D.inches;
}
Distance operator + (const Distance &D)
{
Distance temp;
temp.feet = feet + D.feet;
temp.inches = inches + D.inches;
return temp;
}
// method to display distance
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main()
{
Distance D1(11, 10), D2(5, 11),D3;
cout << "First Distance : ";
D1.displayDistance();
cout << "Second Distance :";
D2.displayDistance();
// use assignment operator
D1 = D2;
cout << "First Distance :";
D1.displayDistance();
//use + operator
D3=D1 + D2;
D3.displayDistance();
return 0;
}
Practical Output

First Distance : F: 11 I:10


Second Distance :F: 5 I:11
First Distance :F: 5 I:11
F: 10 I:22

PRACTICAL SET-7 (Inheritance)

20) Create a class named ‘SHOW’ having 2 data members


Production_House_Name and Show_name. Extend 2 classes viz. ‘MOVIE’
and ‘SERIES’ from the class ‘SHOW’. Class MOVIE has a data member
named Movie_hours and SERIES has a data member
Number_of_Episodes. Create object of MOVIE and SERIES. Create
necessary member function in each class.

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.

You might also like