CPP_Practical
CPP_Practical
Assignment NO 1
Implement a class Complex which represents the Complex Number data type.
1. Constructor (including a default constructor which creates the complex number 0+0i).
#include<iostream>
class complex
{
float x;
float y;
public:
complex()
{
x=0;
y=0;
}
complex operator+(complex);
complex operator*(complex);
friend istream &operator >>(istream &input,complex &t)
{
cout<<"Enter the real part";
input>>t.x;
cout<<"Enter the imaginary part";
input>>t.y;
}
friend ostream &operator <<(ostream &output,complex &t)
{
output<<t.x<<"+"<<t.y<<"i\n";
}
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
complex complex::operator*(complex c)
{
complex temp2;
temp2.x=(x*c.x)-(y*c.y);
temp2.y=(y*c.x)+(x*c.y);
return (temp2);
}
int main()
{
complex c1,c2,c3,c4;
cout<<"Default constructor value=\n";
cout<<c1;
cout<<"\nEnter the 1st number\n";
cin>>c1;
cout<<"\nEnter the 2nd number\n";
cin>>c2;
c3=c1+c2;
c4=c1*c2;
cout<<"\nThe first number is ";
cout<<c1;
cout<<"\nThe second number is ";
cout<<c2;
cout<<"\nThe addition is ";
cout<<c3;
cout<<"\nThe multiplication is ";
cout<<c4;
return 0;
}
[4] Write a C++ program create a calculator for an arithmetic operator (+, -, *, /).
The program should take two operands from user and performs the operation on those
two operands depending upon the operator entered by user. Use a switch statement to
select the operation. Finally, display the result.
Some sample interaction with the program might look like this:
Enter first number, operator, second number: 10 / 3 Answer = 3.333333 Do another (y/n)?
y
Enter first number, operator, second number: 12 + 100 Answer = 112 Do another (y/n)? n
#include <iostream>
using namespace std;
int main() {
char choice;
do {
double num1, num2, result;
char op;
return 0;
}
Assignment No 3
Implement a class Cpp Array which is identical to a one-dimensional C++ array (i.e., the index set is
a set of consecutive integers starting at 0) except for the following: 1. It performs range checking.
2. It allows one to be assigned to another array through the use of the assignment operator (e.g.
cp1= cp2) 3. It supports a function that returns the size of the array. 4. It allows the reading or
printing of array through the use of cout and cin
#include <iostream>
class CppArray {
private:
public:
CppArray(int s) {
if (s <= 0) {
size = s;
~CppArray() {
delete[] arr;
size = other.size;
arr[i] = other.arr[i];
if (this == &other) {
delete[] arr;
size = other.size;
arr[i] = other.arr[i];
return *this;
return arr[index];
return size;
return input;
return output;
};
int main() {
int n;
CppArray arr1(n);
try {
std::cout << "Size of the array: " << arr1.getSize() << std::endl;
return 0;
}
Assignment No 5
Develop an object oriented program in C++ to create a database of student information system
containing the following information: Name, Roll number, Class, division, Date of Birth, Blood
group, Contact address, telephone number, driving licence no. etc Construct the database with
suitable member functions for initializing and destroying the data viz constructor, default
constructor, Copy constructor, destructor, static member functions, friend class, this pointer, inline
code and dynamic memory allocation operators-new and delete.
#include <iostream>
#include <string>
class StudData;
class Student{
string name;
int roll_no;
string cls;
char* division;
string dob;
char* bloodgroup;
public:
name="";
roll_no=0;
cls="";
division=new char;
dob="dd/mm/yyyy";
bloodgroup=new char[4];
}
~Student()
delete division;
delete[] bloodgroup;
return count;
void getData(StudData*);
void dispData(StudData*);
};
class StudData{
string caddress;
public:
StudData()
caddress="";
telno=new long;
dlno=new long;
}
~StudData()
delete telno;
delete dlno;
void getStudData()
cin.get();
getline(cin,caddress);
cin>>*telno;
cin>>*dlno;
void dispStudData()
};
getline(cin,name);
cin.get();
getline(cin,cls);
cin>>division;
cin.get();
getline(cin,dob);
cin>>bloodgroup;
st->getStudData();
count++;
cout<<"Class : "<<cls<<endl;
cout<<"Division : "<<division<<endl;
st1->dispStudData();
int Student::count;
int main()
Student* stud1[100];
StudData* stud2[100];
int n=0;
char ch;
do
stud1[n]=new Student;
stud2[n]=new StudData;
stud1[n]->getData(stud2[n]);
n++;
cin>>ch;
cin.get();
for(int i=0;i<n;i++)
cout<<"---------------------------------------------------------------"<<endl;
stud1[i]->dispData(stud2[i]);
cout<<"---------------------------------------------------------------"<<endl;
cout<<endl<<"---------------------------------------------------------------"<<endl;
for(int i=0;i<n;i++)
delete stud1[i];
delete stud2[i];
return 0;
}
Assignment No 8
Imagine a publishing company which does marketing for book and audiocassette versions. Create
a class publication that stores the title (a string) and price (type float) of a publication. From this
class derive two classes: book, which adds a page count (type int), and tape, which adds a playing
time in minutes (type float). Write a program that instantiates the book and tape class, allows user
to enter data and displays the data members. If an exception is caught, replace all the data
member values with zero values
#include <iostream>
#include <string>
#include <stdexcept>
class Publication {
protected:
std::string title;
float price;
public:
// Default constructor
std::getline(std::cin, title);
if (price < 0) {
std::cout << "Title: " << title << "\nPrice: $" << price << std::endl;
title = "";
price = 0.0f;
};
private:
int pageCount;
public:
// Default constructor
Publication::getData(); // Call the base class method to get title and price
pageCount = 0;
};
private:
float playTime;
public:
// Default constructor
Publication::getData(); // Call the base class method to get title and price
if (playTime < 0) {
std::cout << "Playing Time: " << playTime << " minutes" << std::endl;
playTime = 0.0f;
};
int main() {
Book myBook;
Tape myTape;
try {
myBook.getData();
std::cerr << "Error: " << e.what() << "\nResetting values to zero...\n";
myBook.reset();
}
std::cin.ignore(); // To clear the newline character from the input buffer
try {
myTape.getData();
std::cerr << "Error: " << e.what() << "\nResetting values to zero...\n";
myTape.reset();
myBook.displayData();
myTape.displayData();
return 0;
Assignment no 11:
A book shop maintains the inventory of books that are being sold at the shop. The list includes
details such as author, title, price, publisher and stock position. Whenever a customer wants a
book, the sales person inputs the title and author and the system searches the list and displays
whether it is available or not. If it is not, an appropriate message is displayed. If it is, then the
system displays the book details and requests for the number of copies required. If the
requested copies book details and requests for the number of copies required. If the requested
copies are available, the total cost of the requested copies is displayed; otherwise the message
Required copies not in stock‖ is displayed. Design a system using a class called books with
suitable member functions and Constructors. Use new operator in constructors to allocate
memory space required. Implement C++ program for the system
#include<iostream>
#include<string.h>
#include<stdlib.h>
class book
char author[20];
char title[20];
char publisher[20];
double price;
int stock;
public:
book();
void insertdata();
void display();
int search(char[],char[]);
void nocopies(int);
};
book::book()
price=0;
stock=0;
void book::insertdata()
{
cin>>title;
cin>>author;
cin>>publisher;
cin>>price;
cin>>stock;
void book::display()
if(strcmp(title,t)&&(strcmp(author,a)))
return 0;
else
return 1;
if(stock>=num)
else
int main()
int ch,n,i,flag=0,copies,key=0;
book b[100];
char bname[50];
char key_title[50],key_author[50];
do
cout<<"\n*****Book Store******";
cin>>ch;
switch(ch)
case 1:
cin>>n;
for(i=0;i<n;i++)
b[i].insertdata();
}
break;
case 2:
cout<<"\n"<<"TITLE"<<"\t \t "<<"AUTHOR"<<"\t\t"<<"PUBLISHER"<<"\t\
t"<<"PRICE"<<"\t "<<"STOCK";
for(i=0;i<n;i++)
cout<<"\n";
b[i].display();
break;
case 3:
cin>>key_title;
cin>>key_author;
for(i=0;i<n;i++)
if(b[i].search(key_title,key_author))
flag=1;
cout<<"\n"<<"TITLE"<<"\t \t "<<"AUTHOR"<<"\t\t"<<"PUBLISHER"<<"\t\t"<<"PRICE"<<"\t
"<<"STOCK";
b[i].display();
//break;
key=i;
}
if(flag==1)
else
break;
if(flag==1)
cin>>copies;
b[key].nocopies(copies);
break;
case 4: exit(EXIT_SUCCESS);
break;
default :
break;
}while(ch!=5);
return 0;
Assignment no:12
Assume appropriate data members and member function to accept required data & print bio data.
Create biodata using multiple inheritance using C++.
*/
#include<iostream>
class personal
public:
char name[20];
int age;
int date,month,year;
char gender;
personal()
cin>>name;
cin>>age;
cin>>date>>month>>year;
cin>>gender;
};
class professional
public:
int id;
int d,m,y;
professional()
cin>>id;
cin>>d>>m>>y;
};
class academic
public:
char edu[20];
char branch[20];
academic()
cin>>edu;
cin>>branch;
};
public:
all()
cout<<"\n\tBIO DATA\n";
cout<<"Personal data:";
cout<<"\n\nprofessional data:\n";
cout<<"\n\nacademic data:\n";
};
int main()
all obj;
return 0;
}
GROUP B
Assignment No 13
Crete User defined exception to check the following conditions and throw the exception if the
criterion does not meet. a. User has age between 18 and 55 b. User stays has income between Rs.
50,000 – Rs. 1,00,000 per month c. User stays in Pune/ Mumbai/ Bangalore / Chennai d. User has
4-wheeler Accept age, Income, City, Vehicle from the user and check for the conditions mentioned
above. If any of the condition not met then throw the exception
#include <iostream>
#include <string>
#include <stdexcept>
public:
};
public:
};
};
public:
};
class User {
private:
int age;
double income;
std::string city;
bool hasFourWheeler;
public:
void getData() {
std::getline(std::cin, city);
std::cout << "Do you have a 4-wheeler (1 for Yes, 0 for No): ";
void validate() {
throw AgeException();
throw IncomeException();
if (city != "Pune" && city != "Mumbai" && city != "Bangalore" && city != "Chennai") {
throw CityException();
if (!hasFourWheeler) {
throw VehicleException();
};
int main() {
User user;
try {
std::cout << "User data is valid and meets all criteria." << std::endl;
return 0;