Object Oriented Programming With C++
Object Oriented Programming With C++
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"
int main(){
int a;
cin >> a;
float s = 0;
if (i%2 == 1){
float x = i;
}else{
float x = i;
cout << s;
}
2. Write a program to remove the duplicates from an array.
#include <iostream>
#include <vector>
int main(){
vector<int> un;
if(arr[i] != arr[i+1]){
un.push_back(arr[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.
#include <iostream>
#include<map>
#include<string>
#include<iterator>
int main(){
string s;
getline(cin, s);
map<char,int> dic;
dic[s[i]]++;
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):
#include <iostream>
#include <string>
void Address(){
string a;
cout<<"enter string"<<endl;
cin>>a;
for(int i=0;i<a.length();i++){
void concatenate(){
string c;
string b;
cin>>c;
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++;
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];
void reverse(){
string str4;
cout<<"enter string"<<endl;
cin>>str4;
for(int i=str4.length();i>-1;i--){
str3.push_back(str4[i]);
void insert(){
string f;
string g;
int h;
string u;
cout<<"enter string"<<endl;
cin>>f;
cin>>g;
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]);
int main() {
int x;
cout<<"enter 1 to find adress"<<endl;
cout<<"enter 2 to concatenate"<<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>
a[j] = a[j+1];
a[j+1]= temp;
return 0;
int main(){
int m,n;
cin >> m;
cin >> n;
int c[m+n];
c[i] = a[i];
c[m+i] = b[i];
bubblesort(c, m+n);
}
6. Write a program to search a given element in a set of N numbers.
#include <iostream>
int main(){
int k;
cin >> k;
int flag = 0;
if(arr[i] == k){
flag = 1;
break;
if(flag == 0){
}
7. Write a program to calculate GCD of two numbers.
#include <iostream>
if (b == 0){
return a;
return gcd(b,a%b);
int main(){
int 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>
class Matrix{
int **mat;
public:
rows = r;
cols = c;
void input(){
void display(){
return temp;
if (cols != m.rows){
temp.mat[i][j] = 0;
}
return temp;
Matrix transpose(){
temp.mat[i][j] = mat[j][i];
return temp;
};
int main(){
m1.input();
m2.input();
try{
Matrix m3 = m1 + m2;
m3.display();
try{
Matrix m4 = m1 * m2;
m4.display();
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>
class Person
protected:
string name;
public:
Person(string name)
this->name = name;
};
{
private:
string course;
int marks;
int year;
public:
this->course = course;
this->marks = marks;
this->year = year;
void display()
Person::display();
};
private:
string department;
int salary;
public:
this->department = department;
this->salary = salary;
void display()
Person::display();
};
int main(){
Person *p;
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>
class Triangle{
private:
float a,b,c;
public:
a = x;
b = y;
c = z;
float area(){
float s = (a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
return (x*y)/2; }
float geta(){
return a; }
float getb(){
return b;
float getc(){
return c;
};
int main(){
float a,b,c;
try{
throw 1;
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;
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>
int main() {
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;