0% found this document useful (0 votes)
93 views24 pages

Object Oriented Programming With C++

The document contains C++ programs to demonstrate various object oriented programming concepts like inheritance, polymorphism, exception handling etc. It includes 10 programs with increasing complexity: 1. A program to compute the sum of a series. 2. Removing duplicates from an array. 3. Printing character occurrences in a text. 4. Menu driven string manipulation programs. 5. Merging two sorted arrays. 6. Searching an element in an array. 7. Calculating GCD of two numbers. 8. Matrix operations using a Matrix class. 9. Demonstrating inheritance and polymorphism with Person, Student, Employee classes. 10. Triangle class with exception handling and overloaded area

Uploaded by

Apeksha Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
93 views24 pages

Object Oriented Programming With C++

The document contains C++ programs to demonstrate various object oriented programming concepts like inheritance, polymorphism, exception handling etc. It includes 10 programs with increasing complexity: 1. A program to compute the sum of a series. 2. Removing duplicates from an array. 3. Printing character occurrences in a text. 4. Menu driven string manipulation programs. 5. Merging two sorted arrays. 6. Searching an element in an array. 7. Calculating GCD of two numbers. 8. Matrix operations using a Matrix class. 9. Demonstrating inheritance and polymorphism with Person, Student, Employee classes. 10. Triangle class with exception handling and overloaded area

Uploaded by

Apeksha Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

OBJECT ORIENTED

PROGRAMMING
WITH C++

Practical File

Name - VINEET
Roll No - 22566
Submitted To - Kirti Ma’am
1. Write a program to compute the sum of the first n terms of the following series:

#include <iostream>

#include "math.h"

using namespace std;

int main(){

int a;

cout << "Enter n : ";

cin >> a;

float s = 0;

for(int i = 1; i <= a; i++){

if (i%2 == 1){

float x = i;

s += ((float) 1/ (float) powf(x,x));

}else{

float x = i;

s -= ((float) 1/ (float) powf(x,x));

cout << s;

}
2. Write a program to remove the duplicates from an array.

#include <iostream>

using namespace std;

#include <vector>

int main(){

vector<int> un;

int arr[12] = {1,2,3,4,5,6,7,7,8,8,9,9};

for(int i = 0; i < 12; i++){

if(arr[i] != arr[i+1]){

un.push_back(arr[i]);

cout << "Size " << un.size() << endl;

cout << "Capacity " << un.capacity() << endl;

for(int i = 0; i < un.size(); i++){

cout << un[i] << " ";

}
3. Write a program that prints a table indicating the number of occurrences of each alphabet in the text entered as
command line arguments.

// Dictionary of saving count of every character

#include <iostream>

#include<map>

#include<string>

#include<iterator>

using namespace std;

int main(){

string s;

getline(cin, s);

map<char,int> dic;

for(int i=0; i < s.length(); i++){

dic[s[i]]++;

for (auto i : dic)

cout << i.first << " \t\t " << i.second << endl;

return 0;

}
4. Write a menu driven program to perform string manipulation (without using inbuilt string functions):

a. Show address of each character in string


b. Concatenate two strings.
c. Compare two strings
d. Calculate length of the string (use pointers)
e. Convert all lowercase characters to uppercase
f. Reverse the string
g. Insert a string in another string at a user specified position

#include <iostream>

#include <string>

using namespace std;

void Address(){

string a;

cout<<"enter string"<<endl;

cin>>a;

for(int i=0;i<a.length();i++){

cout<<"address of"<< &(a[i]) <<"is"<<i<<endl;

void concatenate(){

string c;

string b;

cout<<"enter 1st string"<<endl;

cin>>c;

cout<<"enter 2nd string"<<endl;

cin>>b;

cout<<(string)c+" "+b<<endl;

void length(){

string d;
int leng=0;

cout<<"enter string"<<endl;

cin>>d;

for(int j=0;j<d.length();j++){

leng++;

cout<<"length of string is "<<leng<<endl;

void upper(){

string e;

string str1;

string str2;

str1="abcdefghijklmnopqrstuvwxyz";

str2="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

cout<<"enter string"<<endl;

cin>>e;

for(int i=0;i<e.length();i++){

for(int j=0;j<str1.length();j++){

if (e[i]==str1[j]){

e[i]=str2[j];

cout<<"capitalized string is"<<e<<endl;

void reverse(){

string str4;

string str3=" ";

cout<<"enter string"<<endl;

cin>>str4;

for(int i=str4.length();i>-1;i--){
str3.push_back(str4[i]);

cout<<"reversed string is"<<str3<<endl;

void insert(){

string f;

string g;

int h;

string u;

cout<<"enter string"<<endl;

cin>>f;

cout<<"enter the string to be inserted"<<endl;

cin>>g;

cout<<"enter the index of insertion"<<endl;

cin>>h;

for(int i=0;i<h;i++){

u.push_back(f[i]);

for(int j=0;j<g.length();j++){

u.push_back(g[j]);

for(int k=h;k<f.length();k++){

u.push_back(f[k]);

cout<<"morphed string is"<<" "<<u<<endl;

int main() {

int x;
cout<<"enter 1 to find adress"<<endl;

cout<<"enter 2 to concatenate"<<endl;

cout<<"enter 3 to find length"<<endl;

cout<<"enter 4 to convert to uppercase"<<endl;

cout<<"enter 5 to reverse the string"<<endl;

cout<<"enter 6 to execute insertion"<<endl;

cin>>x;

switch(x){

case 1:

if(x==1){

Address();

break;

case 2:

if (x==2){

concatenate();

break;

case 3:

if(x==3){

length();

break;

case 4:

if(x==4){

upper();

break;

case 5:

if(x==5){

reverse();
break;

case 6:

if(x==6){

insert();

break;

return 0;

}
5. Write a program to merge two ordered arrays to get a single ordered array.

#include <iostream>

using namespace std;

int bubblesort(int a[], int n){

for (int i = 0; i < n-1; i++){

for (int j = 0; j < n-i-1; j++){

if (a[j] > a[j+1]){

int temp = a[j];

a[j] = a[j+1];

a[j+1]= temp;

return 0;

int main(){

int m,n;

cout << "Enter the size of first array : ";

cin >> m;

cout << "Enter the size of second array : ";

cin >> n;

int a[m], b[n];

cout << "Enter the elements of first array : ";

for (int i = 0; i < m; i++){

cin >> a[i];

cout << "Enter the elements of second array : ";

for (int i = 0; i < n; i++){


cin >> b[i];

int c[m+n];

for (int i = 0; i < m; i++){

c[i] = a[i];

for (int i = 0; i < n; i++){

c[m+i] = b[i];

cout << "The merged array is : ";

for (int i = 0; i < m+n; i++){

cout << c[i] << " ";

bubblesort(c, m+n);

cout << "The sorted merged array is : ";

for (int i = 0; i < m+n; i++){

cout << c[i] << " ";

cout << endl;

}
6. Write a program to search a given element in a set of N numbers.

#include <iostream>

using namespace std;

int main(){

int arr[10] = {1,2,3,4,5,6,7,8,9,10};

int k;

cout << "Enter Number to Search : ";

cin >> k;

int flag = 0;

for(int i = 0; i < 10; i++){

if(arr[i] == k){

cout << "Found at " << i << endl;

flag = 1;

break;

if(flag == 0){

cout << "Not Found" << endl;

}
7. Write a program to calculate GCD of two numbers.

#include <iostream>

using namespace std;

int gcd(int a,int b){

if (b == 0){

return a;

return gcd(b,a%b);

int main(){

int a, b;

cout << "Enter Both Numbers : ";

cin >> a >> b;

cout << gcd(a,b);

}
8. Create a Matrix class. Write a menu-driven program to perform following Matrix operations (exceptions should be
thrown by the functions if matrices passed to them are incompatible and handled by the main() function):

a. Sum
b. Product
c. Transpose

#include <iostream>

using namespace std;

class Matrix{

int **mat;

int rows, cols;

public:

Matrix(int r, int c){

rows = r;

cols = c;

mat = new int*[rows];

for (int i = 0; i < rows; i++){

mat[i] = new int[cols];

void input(){

cout << "Enter Matrix Elements : " << endl;

for (int i = 0; i < rows; i++){

for (int j = 0; j < cols; j++){

cin >> mat[i][j];

void display(){

cout << "Matrix is : " << endl;


for (int i = 0; i < rows; i++){

for (int j = 0; j < cols; j++){

cout << mat[i][j] << " ";

cout << endl;

Matrix operator+(Matrix m){

if (rows != m.rows || cols != m.cols){

throw "Matrices are not compatible for addition";

Matrix temp(rows, cols);

for (int i = 0; i < rows; i++){

for (int j = 0; j < cols; j++){

temp.mat[i][j] = mat[i][j] + m.mat[i][j];

return temp;

Matrix operator*(Matrix m){

if (cols != m.rows){

throw "Matrices are not compatible for multiplication";

Matrix temp(rows, m.cols);

for (int i = 0; i < rows; i++){

for (int j = 0; j < m.cols; j++){

temp.mat[i][j] = 0;

for (int k = 0; k < cols; k++){

temp.mat[i][j] += mat[i][k] * m.mat[k][j];

}
return temp;

Matrix transpose(){

Matrix temp(cols, rows);

for (int i = 0; i < cols; i++){

for (int j = 0; j < rows; j++){

temp.mat[i][j] = mat[j][i];

return temp;

};

int main(){

int r1, c1, r2, c2;

cout << "Enter rows and columns of first matrix : ";

cin >> r1 >> c1;

cout << "Enter rows and columns of second matrix : ";

cin >> r2 >> c2;

Matrix m1(r1, c1), m2(r2, c2);

m1.input();

m2.input();

try{

Matrix m3 = m1 + m2;

m3.display();

catch(const char *s){

cout << s << endl;

try{

Matrix m4 = m1 * m2;
m4.display();

catch(const char *s){

cout << s << endl;

Matrix m5 = m1.transpose();

m5.display();

return 0;

}
9. Define a class Person having name as a data member. Inherit two classes Student and Employee from Person. Student
has additional attributes as course, marks and year and Employee has department and salary. Write display() method in
all the three classes to display the corresponding attributes. Provide the necessary methods to show runtime
polymorphism.

// Define a class Person having name as a data member. Inherit two classes Student and
Employee from Person. Student has additional attributes as course, marks and year and
Employee has department and salary. Write display() method in all the three classes to
display the corresponding attributes. Provide the necessary methods to show runtime
polymorphism.

#include <iostream>

#include <string>

using namespace std;

class Person

protected:

string name;

public:

Person(string name)

this->name = name;

virtual void display()

cout << "Name: " << name << endl;

};

class Student : public Person

{
private:

string course;

int marks;

int year;

public:

Student(string name, string course, int marks, int year) : Person(name)

this->course = course;

this->marks = marks;

this->year = year;

void display()

Person::display();

cout << "Course: " << course << endl;

cout << "Marks: " << marks << endl;

cout << "Year: " << year << endl;

};

class Employee : public Person

private:

string department;

int salary;

public:

Employee(string name, string department, int salary) : Person(name)

this->department = department;
this->salary = salary;

void display()

Person::display();

cout << "Department: " << department << endl;

cout << "Salary: " << salary << endl;

};

int main(){

Person *p;

Student Ramesh("Ramesh", "B.Tech", 90, 2019);

p = &Ramesh;

p->display();

}
10. Create a Triangle class. Add exception handling statements to ensure the following conditions: all sides are greater
than 0 and sum of any two sides is greater than the third side. The class should also have overloaded functions for
calculating the area of a right angled triangle as well as using Heron's formula to calculate the area of any type of
triangle.

#include <iostream>

#include <cmath>

using namespace std;

class Triangle{

private:

float a,b,c;

public:

Triangle(float x, float y, float z){

a = x;

b = y;

c = z;

float area(){

float s = (a+b+c)/2;

return sqrt(s*(s-a)*(s-b)*(s-c));

float area(float x, float y){

return (x*y)/2; }

float geta(){

return a; }

float getb(){

return b;

float getc(){

return c;

};
int main(){

float a,b,c;

cout << "Enter the sides of the triangle: ";

cin >> a >> b >> c;

try{

if(a <= 0 || b <= 0 || c <= 0){

throw 1;

if(a+b <= c || b+c <= a || a+c <= b){

throw 2;

}catch(int x){

if(x == 1){

cout << "Sides cannot be less than or equal to 0" << endl;

return 0;

if(x == 2){

cout << "Sum of any two sides cannot be less than or equal to the third side" << endl;

return 0;

Triangle t(a,b,c);

cout << "Area of the triangle is: " << t.area() << endl;

if(t.geta()*t.geta() + t.getb()*t.getb() == t.getc()*t.getc() || t.geta()*t.geta() +


t.getc()*t.getc() == t.getb()*t.getb() || t.getb()*t.getb() + t.getc()*t.getc() ==
t.geta()*t.geta()){

cout << "Area of the right angled triangle is: " << t.area(t.geta(), t.getb()) <<
endl;

return 0;

}
11. Copy the contents of one text file to another file, after removing all whitespaces.

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

int main() {

string inputFileName = "input.txt";

string outputFileName = "output.txt";

ifstream inputFile(inputFileName);

if (!inputFile) {

cerr << "Error opening input file: " << inputFileName << endl;

return 1;

ofstream outputFile(outputFileName);

if (!outputFile) {

cerr << "Error opening output file: " << outputFileName << endl;

return 1;

char ch;

while (inputFile.get(ch)) {

if (!isspace(ch)) {

outputFile.put(ch);

}
inputFile.close();

outputFile.close();

cout << "Contents of " << inputFileName << " copied to " << outputFileName << " with
whitespaces removed." << endl;

return 0;

You might also like