You are on page 1of 63

Object Oriented Programming using

C++ LAB
BCA 2nd Year, 3rd Semester
SECTION – B

Submitted To
Mr. Piyush Kumar Gupta
(Assistant Professor)

Submitted By:
Mohd Shifan Saud
2020-301-086

(Signature)

JAMIA HAMDARD
Department of Computer Science & Engineering
School of Engineering Sciences & Technology
New Delhi-110062
INDEX
Sl. Signatur
C++ Programs
No. e

Write a program to display the following output using a single


cout statement.

Subject Marks
1.
Mathematics 90

Computer 77

Chemistry 69

Write a program which accept temperature in Fahrenheit and


2.
print it in centigrade.

Write a program which accept principle, rate and time from user
3.
and print the simple interest.

Write a program which accepts a character and display its ASCII


4.
value.

Write a program to swap the values of two variables using call


5.
by value and call by reference.

Write a program to calculate area of circle, square, rectangle and


6.
triangle.

Write a program to check whether the given number is positive


7.
or negative (using ? : ternary operator ).

Define a class String. Use overloaded == operator to compare


8.
two strings.

9. Write a program which accepts amount as integer and display


total number of Notes of Rs. 500, 100, 50, 20, 10, 5 and 1.

For example, when user enters a number, 575, the results would
be like this...

500: 1

100: 0
50: 1

20: 1

10: 0

5: 1

1: 0

Write a program which accepts days as integer and display total


number of years, months and days in it.
10.
For example: If user input as 856 days the output should be 2
years 4 months 6 days.

Any year is input by the user. Write a program to determine


11.
whether the year is a leap year or not.

Write a program to calculate the monthly telephone bills as per


the following rule:

Minimum Rs. 200 for upto 100 calls.


12.
Plus Rs. 0.60 per call for next 50 calls.

Plus Rs. 0.50 per call for next 50 calls.

Plus Rs. 0.40 per call for any call beyond 200 calls.

Any character is entered by the user; write a program to


determine whether the character entered is a capital letter, a
13.
small case letter, a digit or a special symbol. (Hint: Use ASCII
values)

Write a function that receives two numbers as an argument and


14. display all prime numbers between these two numbers. Call this
function from main( ).

Write a C++ program to find the sum and average of one


15.
dimensional array using pointer.

Create a class FLOAT that contains one float data member.


16. Overload all the four arithmetic operations so that they operate
on the objects of FLOAT.

Write a C++ program to reverse the element of an integer 1-D


17.
array.
Write a C++ program to find the largest and smallest element of
18.
an array using function.

19. Write a program to find the length of string.

20. Write a program to count number of words in string.

21. Write a program to check a string is palindrome or not.

Write a menu driven C++ program to do following operation on


two dimensional array A of size m x n. You should use user-
defined functions which accept 2-D array A, and its size m and n
as arguments. The options are:

To input elements into matrix of size m x n

22. To display elements of matrix of size m x n

Sum of all elements of matrix of size m x n

To display row-wise sum of matrix of size m x n

To display column-wise sum of matrix of size m x n

To create transpose of matrix B of size n x m

Write a program to multiply 2-D array A and B of order NxL and


23.
LxM.

24. Define a class student with the following specification

Private members of class student

admno integer

sname 20 character

eng. math, science float

total float

ctotal() a function to calculate eng + math + science


with float return type.

Public member function of class student

Takedata() Function to accept values for admno,


sname, eng, science and invoke ctotal() to calculate total.

Showdata() Function to display all the data members


on the screen.

Write the definition for a class called complex that has floating
point data members for storing real and imaginary parts. The
class has the following member functions:

void set(float, float) to set the specified value in object

void disp() to display complex number object


25. complex sum(complex) to sum two complex numbers & return
complex number

1. Write the definitions for each of the above member functions.

2. Write main function to create three complex number objects.


Set the value in two objects and call sum() to calculate sum and
assign it in third object. Display all complex numbers.

26. Consider the definition of the following class:

class Sample

private:

int x;

double y;

public :

Sample(); //Constructor 1

Sample(int); //Constructor 2

Sample(int, int); //Constructor 3

Sample(int, double); //Constructor 4

};

i. Write the definition of the constructor 1 so that the private


member variables are initialized to 0.

ii. Write the definition of the constructor 2 so that the private


member variable x is initialized according to the value of the
parameter, and the private member variable y is initialized to 0.

iii. Write the definition of the constructors 3 and 4 so that the


private member variables are initialized according to the values
of the parameters.

Write a menu driven program for bank user who has two a/c
saving and current where he can check the balance, update the
27.
balance and withdraw the amount, and calculate the interest
using inheritance concept.

Write programs to demonstrate the use of virtual functions,


28.
virtual classes and pure-virtual functions.

Create two classes named Mammals and MarineAnimals. Create


another class named BlueWhale which inherits both the above
classes. Now, create a function in each of these classes which
prints "I am mammal", "I am a marine animal" and "I belong to
both the categories: Mammals as well as Marine Animals"
respectively. Now, create an object for each of the above class
29. and try calling

1 - function of Mammals by the object of Mammal

2 - function of MarineAnimal by the object of MarineAnimal

3 - function of BlueWhale by the object of BlueWhale

4 - function of each of its parent by the object of BlueWhale

Create a class named Shape with a function that prints "This is a


shape". Create another class named Polygon inheriting the Shape
class with the same function that prints "Polygon is a shape".
Create two other classes named Rectangle and Triangle having
the same function which prints "Rectangle is a polygon" and
30. "Triangle is a polygon" respectively. Again, make another class
named Square having the same function which prints "Square is
a rectangle".

Now, try calling the function by the object of each of these


classes.

31. Create a class named 'Rectangle' with two data members- length
and breadth and a function to calculate the area which is
'length*breadth'. The class has three constructors which are :

1 - having no parameter - values of both length and breadth are


assigned zero.

2 - having two numbers as parameters - the two numbers are


assigned as length and breadth respectively.

3 - having one number as parameter - both length and breadth


are assigned that number.

Now, create objects of the 'Rectangle' class having none, one and
two parameters and print their areas.

Write a program to overload the function to find the sum of


32. three numbers, discriminate factor (b^2-4ac) and area of the
circle using inline functions.

Write a macro to calculate simple interest from principal, rate of


33. interest and time.

Simple interest = (principal*rate of interest*time)/100.

Write a program to print the name, salary and date of joining of


34.
10 employees in a company. Use array of objects.

Write a program that takes your full name as input and displays
the abbreviations of the first and middle names except the last
35.
name which is displayed as it is. For example, if your name is
Robert Brett Roser, then the output should be R.B.Roser.

Create a class with a function that prints "This is parent class"


and its subclass with another function that prints "This is child
class". Now, create an object for each class and call

1 - function of the parent class by the object of the parent class


36.
2 - function of the child class by the object of the child class

3 - function of the parent class by the object of the child class

4- function of the child class by the pointer of the base class.


Q1.

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
    int sub1,sub2,sub3;
    cout<<"Enter Mathematics marks = ";
    cin>>sub1;
    cout<<"Enter Computer marks = ";
    cin>>sub2;
    cout<<"Enter Chemistry marks = ";
    cin>>sub3;
    cout<<"\nSubject\t\t\tMarks\n";
    cout<<"\nMathematics\t\t"<<sub1;
    cout<<"\nComputer\t\t"<<sub2;
    cout<<"\nChemistry\t\t"<<sub3;
    getch();
}
Q2
#include<iostream>
using namespace std;
int main()
{
float centigrade;
float fahrenheit;
cout<<"Enter temp. in Fahrenheit: ";
cin>>fahrenheit;
cout<<endl;
centigrade = (fahrenheit - 32) * 5/9;

cout<<"Temp. in Celsius: "<<centigrade;


return 0;
}

Output:
Enter temp. in Fahrenheit: 93
Temp. in Celsius: 33.8889
Q3.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
   
    int principle,rate,time,intrest;
    cout<<"Enter Principle = ";
    cin>>principle;
    cout<<"Enter Rate = ";
    cin>>rate;
    cout<<"Enter Time = ";
    cin>>time;
    intrest=(principle*time*rate)/100;
    cout<<"The intrest rate is "<<intrest;
getch();
}

OUTPUT
Enter Principle = 1300

Enter Rate = 7

Enter Time = 54

The interest rate is 4914


Q4.#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
   
    char ch;
    cout<<"Please Enter a character and check its ASCII Value ";
    cin>>ch;
    cout<<"ASCII Value of "<<ch<<" is = "<<(int)ch;
getch();
}
Q5. #include<iostream>

using namespace std;

void swap(int &x, int &y)

int temp;

temp = x;

x = y;

y = temp;

int main()

int a, b;

cout<<"Enter the value of a: ";

cin>>a;

cout<<"Enter the value of b: ";

cin>>b;

cout<<endl<<"Before swapping: ";

cout<<"a= "<<a<<" and b= "<<b;

swap(a, b);

cout<<endl<<"After swapping: ";

cout<<"a= "<<a<<" and b= "<<b;

}
Q6.

OUTPUT
Q7 #include <iostream>

using namespace std;

static void CheckNumber(double);

static void CheckNumber(double x) {

string message;

message = (x > 0)? "Positive number" :

(x == 0)? "Zero" : "Negative number";

cout<<message<<"\n";

int main() {

CheckNumber(5.5);

CheckNumber(-10.8);

return 0;

OUTPUT
Positive number
Negative number
Q8 A sequence of characters can be represented using an object of a class in C++. The class which
provides a definition to do so is called a String class. String header <string> needs to be included in the
program to use the String class. Let us understand how to declare and assign a value to a string.

C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is
called function overloading and operator overloading respectively.

#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

class String

char str[20];

public:

void getdata()

gets(str);

int operator ==(String s)

if(!strcmp(str,s.str))

return 1;

return 0;

};

int main()

String s1,s2;

cout<<"Enter first string :: ";

s1.getdata();

cout<<"\nEnter second string :: ";


s2.getdata();

if(s1==s2)

cout<<"\nStrigs are Equal\n";

else

cout<<"\nStrings are Not Equal\n";

return 0;

OUTPUT

Enter first string :: MOhdshifan

Enter second string :: mohdshifan

Strings are Not Equal

Process returned 0
Q9.

OUTPUT
Q 10

OUTPUT
Q 11.

OUTPUT
Q 12.

OUTPUT
Q 13

OUTPUT
Q14
OUTPUT
Q15

OUTPUT
Q16
OUTPUT
Q 17

OUTPUT
Q18

OUTPUT
Q 19

OUTPUT
Q 20
Q 21

OUTPUT
Q22
Q 23
Q 24

OUTPUT
Q25
Q26
i.
Sample :: Sample()
{
x = 0;
y = 0;
}
ii.
Sample :: Sample(int a)
{
x = a;
y = 0;
}
iii.
Sample :: Sample(int a, int b)
{
x = a;
y = b;
}
Sample :: Sample(int a, double b)
{
x = a;
y = b;
}

Q27
Q28

OUTPUT
Q29
OUTPUT
Q30
Output
Q 31

OUTPUT
Q32

OUTPUT
Q33

OUTPUT

You might also like