0% found this document useful (0 votes)
72 views29 pages

C++ Old Solution Program

The document contains C++ programs that demonstrate various object-oriented programming concepts like pass by reference, return by reference, constructors with initializer lists, operator overloading for complex numbers, inheritance, exception handling, stream operators, and file handling using structures. It includes programs to illustrate pass by reference and return by reference, constructors with initializer lists, comparing complex numbers with relational operators, inheriting from a base Person class to create Teacher and Student classes, creating a custom manipulator, catching multiple exceptions using exception classes, reading and displaying Cartesian coordinates and complex numbers using stream operators, and storing student information in a structure and writing it to a binary file.

Uploaded by

Movie Masala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views29 pages

C++ Old Solution Program

The document contains C++ programs that demonstrate various object-oriented programming concepts like pass by reference, return by reference, constructors with initializer lists, operator overloading for complex numbers, inheritance, exception handling, stream operators, and file handling using structures. It includes programs to illustrate pass by reference and return by reference, constructors with initializer lists, comparing complex numbers with relational operators, inheriting from a base Person class to create Teacher and Student classes, creating a custom manipulator, catching multiple exceptions using exception classes, reading and displaying Cartesian coordinates and complex numbers using stream operators, and storing student information in a structure and writing it to a binary file.

Uploaded by

Movie Masala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Old Program Solution for IOE

Pulchowk

Base on Syllabus of Tribhuvan University, Nepal


Example Programs on Object Oriented Programming
(C++)

2062 Baisakh:

//Write a program that illustrate pass by reference and return by


reference.

#include <iostream>

using namespace std;

int &smallest(int &a, int &b) {

return a<b?a:b;

int main() {

int a=5,b=10;
cout << "a = " << a << endl;

cout << "b = " << b << endl;

smallest(a,b) = 50;

cout << "a = " << a << endl;

cout << "b = " << b << endl;

return 0;

// Write a program that illustrates constructors with initializer list.

#include <iostream>

using namespace std;

class Complex {

float real,imag;

public:

Complex():real(0.0),imag(0.0) {}

Complex(float r, float i): real(r),imag(i) {}

void display() {

cout << "(" << real << "," << imag << ")";

};
int main() {

Complex c1(1,2);

c1.display();

return 0;

//Q.6 Write a program to compare complex numbers with relational


operators.

#include <iostream>

#include <cmath>

using namespace std;

class complex {

float real,imag;

public:

complex(float r=0,float i=0) {

real = r;

imag = i;

float magnitude() {

return sqrt(real*real+imag*imag);
}

bool operator <(complex c) {

if (this->magnitude()<c.magnitude())

return true;

return false;

bool operator >(complex c) {

if (this->magnitude()>c.magnitude())

return true;

return false;

bool operator ==(complex c) {

if (this->magnitude()==c.magnitude())

return true;

return false;

void display() {

cout << "(" << real << "," <<imag << ")";

};
int main() {

complex c1(1,2),c2(2,3);

if (c1<c2) {

c1.display();

cout << " is less than ";

c2.display();

} else if (c1>c2) {

c1.display();

cout << " is greater than";

c2.display();

else if (c1==c2) {

c1.display();

cout << " is equal to ";

c2.display();

return 0;

// Q7.Write a program that inherits person class to teacher and


student.
#include <iostream>

using namespace std;

class Person {

char name[25];

int age;

public:

void input() {

cout << "Enter the name: ";

cin >> name;

cout << "Enter the age: ";

cin >> age;

void display() {

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

cout << "Age: " << age << endl;

};

class teacher: public Person {

float salary;
public:

void input() {

cout << "Enter the details of the teacher: " << endl;

Person::input();

cout << "Enter the salary: ";

cin >> salary;

void display() {

Person::display();

cout << "Salary:" << salary;

};

class student: public Person {

float percentage;

public:

void input() {

cout << "Enter the details of the student:" << endl;

Person::input();

cout << "Enter the percentage: ";

cin >> percentage;

}
void display() {

Person::display();

cout << "Percentage: " << percentage << endl;

};

int main() {

student s;

s.input();

s.display();

teacher t;

t.input();

t.display();

return 0;

// Write a program to make your own manipulator.

#include <iostream>

using namespace std;

ostream & rupee(ostream &out) {


out << "Rs.";

return out;

int main() {

int balance;

cout << "Enter your bank balance: ";

cin >> balance;

cout << rupee << balance;

return 0;

// Write a program that catches multiple exceptions using exception


class.

#include <iostream>

using namespace std;

const int size = 4;

class Stack {

int top;

int arr[size];

public:
Stack() {

top = -1;

void push(int x) {

if(top == size-1) {

throw stack_full();

top++;

arr[top] = x;

void pop() {

if(top == -1)

throw stack_empty();

top--;

class stack_full {

public:

char *message;

stack_full() {

message = "Stack overflow";

};
class stack_empty {

public:

char *message;

stack_empty() {

message = "Stack empty";

};

};

int main() {

try {

Stack s;

s.push(1);

s.push(2);

s.push(3);

s.push(4);

s.push(5);

s.pop();

s.pop();

catch (Stack::stack_full sf) {

cout << "Error: " << sf.message;

}
catch (Stack::stack_empty se) {

cout << se.message;

return 0;

2062 Poush:

// Write a program to make distances class and its object. The


distances class has feet and inches.

#include <iostream>

using namespace std;

class distances {

float feet,inches;

public:

void input() {

cout << "Enter the feet and inches: ";

cin >> feet >> inches;

void display() {

cout << feet << " feet and " << inches << " inches";

};
int main() {

distances d;

d.input();

d.display();

return 0;

// Write a program that converts a data of a class with inch and


feet(user defined) to inches(float).

#include <iostream>

using namespace std;

class length {

float feet,inches;

public:

length() {

feet = 0.0;

inches = 0.0;

length(float f,float i) {

feet = f;
inches = i;

operator float() {

float temp;

temp = feet*12 + inches;

return temp;

void display() {

cout << "(" << feet << "," << inches << ")" << endl;

};

int main() {

length d(1,12);

d.display();

float inches;

inches = d;

cout << "Length in inches is: " << inches;

return 0;

}
// Write a program to read and display Cartesian Coordinate using
stream operators.

#include <iostream>

using namespace std;

class Cartesian {

int x,y;

public:

friend istream & operator >>(istream &,Cartesian&);

friend ostream & operator << (ostream &, Cartesian &);

};

istream & operator>>(istream &is,Cartesian &c) {

is >> c.x >> c.y;

return is;

ostream & operator <<(ostream &os,Cartesian &c) {

os<< "(" << c.x << "," << c.y << ")";

return os;

}
int main() {

Cartesian c;

cout << "Enter the cartesian co-ordinate";

cin >> c;

cout << "The entered cartesian co-ordinate is: ";

cout << c;

return 0;

2063 Baisakh:

// Write a program to read and display complex numbers using


stream operator overloading and addition and

// subtraction of complex numbers using operator overloading.

// Hint:a complex number has real and imaginary part as a+ib where
i is (-1)^1/2.

#include <iostream>

using namespace std;

class complex {

float real,imag;

public:

friend istream & operator >>(istream & ,complex &);


friend ostream & operator <<(ostream &,complex &);

complex operator+(complex c) {

complex temp;

temp.real = real+c.real;

temp.imag = imag+c.imag;

return temp;

complex operator-(complex c) {

complex temp;

temp.real = real-c.real;

temp.imag = imag-c.imag;

return temp;

};

istream &operator >>(istream &is,complex &c) {

is >> c.real >> c.imag;

return is;

ostream& operator<<(ostream &os,complex &c) {

os << "(" << c.real << "," << c.imag << ")";

return os;
}

int main() {

complex c1,c2,c3,c4;

cout << "Enter the first complex number ";

cin >> c1;

cout << "Enter the second complex number ";

cin >> c2;

c3 = c1+c2;

cout <<c1 << "+" << c2 << "=" << c3 << endl;

c4 = c1-c2;

cout <<c1 << "-" << c2 << "=" << c4 << endl;

return 0;

2064 Jestha:

// Write a program that stores the information about the


student(student id,DOB,department

// and address) in a structure until user says "no". After reading the
data, write it to a file

// then display the content of the file in the proper format on your
output screen.
#include <iostream>

#include <fstream>

using namespace std;

struct DOB {

int yr,mo,day;

};

struct student {

int student_id;

DOB d;

char department[25];

char address[30];

};

int main() {

student s;

char ch;

fstream fout;

fout.open("students.txt",ios::out | ios::binary | ios::app);

do {

cout << "Enter the student id:";


cin >> s.student_id;

cout << "DOB:";

cin >> s.d.yr;

cin >> s.d.mo;

cin >> s.d.day;

cout << "Department:";

cin >> s.department;

cout << "Address";

cin >> s.address;

fout.write((char *)&s,sizeof(s));

cout << "Do you want to continue? (y/n):";

cin>>ch;

while(ch =='y');

fout.close();

fstream fin;

fin.open("students.txt",ios::in | ios::binary);

while(fin.read((char*)&s,sizeof(s))) {

cout << "Id:" << s.student_id << endl;

cout << "DOB: " << s.d.yr << "/" << s.d.mo << "/" << s.d.day
<< endl;

cout << "Address: " << s.address << endl;


}

fin.close();

return 0;

2064 Falgun:

// Write a program to display n characters by using default argument


for all parameters.

// Assume that the function takes two parameters one character to be


printed and other

// number of characters to be printed.

#include <iostream>

#include <string.h>

using namespace std;

void display(char *p="default text",int n = 12) {

int length = strlen(p);

if(n>length || n<0) {

cout << "Invalid length";

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

cout << p[i];


}

int main() {

display("kathmandu",3);

cout <<"\n";

display("pulchowk",4);

return 0;

2065 Shrawan:

// Write a program to check whether the number is prime or not


using object class concept.

// Remember to return enumerated true or false the function where


you check the number.

#include <iostream>

using namespace std;

enum bol{False,True};

class prime {

int n;

public:

void input() {

cout << "Enter the number:";

cin >> n;
}

bol check() {

bol flag = True;

if(n==2)

return True;

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

if(n%i==0) {

flag = False;

break;

return flag;

void display(){

int flag = check();

if(flag==1)

cout << "It is prime";

else

cout << "It is not prime.";

};
int main() {

prime p;

p.input();

p.display();

return 0;

2068 Baisakh:

/* Write a program to display the output in pyramid form as follows.

AB

ABC

ABCD

*/

#include <iostream>

using namespace std;

int main() {

char *c = "ABCD";

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

cout.write(c,i);
cout << endl;

return 0;

2069 Ashad:

// Write a program that uses a vector container to represent


array and display

// the result in ascending order. For sorting you can use the
vector member functions.

#include <iostream>

#include <vector>

using namespace std;

void sorter(vector<int>&v) {

int temp;

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

for(int j=0;j<v.size()-i-1;j++) {

if(v[j]>v[j+1]) {
temp = v[j];

v[j] = v[j+1];

v[j+1] = temp;

void display(vector<int>&v) {

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

cout << v[i] << "\t";

cout << endl;

int main() {

vector <int>v;

int n;

int elem;

cout << "Enter the number of elements in the vector:";

cin >> n;
cout << "Enter the elements: ";

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

cin >> elem;

v.push_back(elem);

display(v);

sorter(v);

display(v);

return 0;

2070 Chaitra:

// Write a C++ program to join two strings using dynamics


constructor concept

#include <iostream>

#include <string.h>

using namespace std;

class Strings {

char *str;
public:

Strings() {}

Strings(char *s) {

int length = strlen(s);

str = new char[length];

strcpy(str,s);

Strings join(Strings s) {

Strings temp;

int length = strlen(str) + strlen(s.str);

temp.str = new char[length];

strcpy(temp.str,str);

strcat(temp.str,s.str);

return temp;

void display() {

cout << str;

};
int main() {

Strings s1("Amit"),s2("Chaudhary"),s3;

s3 = s1.join(s2);

s3.display();

return 0;

You might also like