Untitled document
Untitled document
Class - SECOMP D
Roll No. - 28
Experiment No: 01
Implement a class Complex which represents the Complex Number
datatype
Implement the following operations:
1. Constructor (including a default constructor which creates the
complex number 0+0i).
2. Overloaded operator + to add two complex numbers.
3. Overloaded operator * to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers. */
#include<iostream>
using namespace std;
class complex
{
float x;float y;
public:
complex() // Default constructor
{
x=0;y=0;
}
complex operator+(complex);//this is declaration of function to
overload + Operator
complex operator*(complex);//function to overload * Operator
friend istream &operator>>(istream &input,complex &t)
{
cout<<"Enter the real part";input>>t.x;
cout<<"Enterthe imaginary part";
input>>t.y;
}
friend ostream &operator<<(ostream &output,complex &t)
{
output<<t.x<<"+"<<t.y<<"i\n";
}
}; //class closing
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;//object of class
cout<<"Default constructor value=\n";
cout<<c1;
cout<<"\n Enter the 1st number\n";
cin>>c1;
cout<<"\n Enter the 2nd number\n";
//cin>>c1;
cin>>c2;
c3=c1+c2;
c4=c1*c2;
cout<<"\nThe firstnumber is";
cout<<c1;
cout<<"\nThe secondnumber is";
cout<<c2;
cout<<"\nThe addition is";
cout<<c3;
cout<<"\nThe multiplication is";
cout<<c4;
return 0;
}
Output
Name - Arya Ranaware
Class - SECOMP D
Roll No. - 28
Experiment No : 2
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 license no. and other.
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>
using namespace std;
class data
{
private:
string name;
char bg;
string address;
string dob;
string cldiv;
string lic;
static int c;
int roll;
long int phone;
public:
data();
static int getcount();
void getdata();
void show();
data(data *obj);
data(int roll,long int phone,string name,string address,string dob,string cldiv, string lic);
~data();
};
int data::c=0;
data::data(data *obj)
{
cout<<"\nCopy constructor implemented"<<endl;
}
data::~data()
{
cout<<"Destructor called \n";
}
void data::getdata()
{
cout<<"Enter roll number \n";
cin>>roll;
cout<<"Enter telephone number \n";
cin>>phone;
cout<<"Enter Name \n";
cin>>name;
cout<<"Enter address \n";
cin>>address;
cout<<"Enter D.O.B \n";
cin>>dob;
cout<<"Enter Class and Division\n";
cin>>cldiv;
cout<<"Enter license number \n";
cin>>lic;
}
void data::show()
{
cout<<"Name :"<<name<<endl;
cout<<"Roll Number :"<<roll<<endl;
cout<<"Telephone Number :"<<phone<<endl;
cout<<"Address :"<<address<<endl;
cout<<"Date of birth :"<<dob<<endl;
cout<<"Class and division: "<<cldiv<<endl;
cout<<"License number :"<<lic<<endl;
}
int data::getcount()
{
return c;
}
int main()
{
int num;
data *d1= new data();
d1->show();
delete d1;
data *d2=new data(73,9823694961,"Vijay","Ravet","26.11.85","SE A","MSIN85U");
d2->show();
data *d3=new data(d2);
d3->show();
delete d2;
cout<<"\nEnter size of database";
cin>>num;
data dx[num];
for(int i=0;i<num;i++)
{
dx[i].getdata();
}
for(int i=0;i<num;i++)
{
dx[i].show();
}
cout<<"Number of constructor calls and total number of objects:"<<data::getcount()<<endl;
return 0;
}
Output
Name - Arya Ranaware
Class - SECOMP D
Roll No. - 28
Experiment No : 3
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 classes, 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.h>
using namespace std;
class Publication
{
protected:
string title;
float price;
public:
void Set_Zero(void) // to set values to zero to handel exception..
{
title="000";
price = 0.0;
}
void getdata(void)
{
cout<<"\n\t Enter the Name: ";
cin>>title;
cout<<"\t Enter the price: ";
cin>>price;
}
void putdata(void)
{
cout<<"\n\t Name: "<<title;
cout<<"\t Price: "<<price;
}
};
class Book : public Publication
{
int page_count;
public:
void Set_Zero(void) // to set values to zero to handel exception..
{
page_count=0;
Publication::Set_Zero();
}
void getdata(void)
{
Publication::getdata();
cout<<"\t Enter the number of pages: ";
cin>>page_count;
try
{
if(price<0)
if(playing_time<0)
throw (0.0); // throwing an double type
Set_Zero();
}
catch(double x)
{
cout<<"\n\t Exception-- Playing time cannot be a negetive value:";
cout<<"\n\t Turning all values to zero.";
Set_Zero();
}
}
void putdata(void)
{
Publication::putdata();
cout<<"\t Plying time: "<<playing_time;
cout<<endl<<endl;
}
};
int main()
{
Tape T;
Book B;
cout<<"\n\t Enter data of Tape :\n";
T.getdata();
T.putdata();
cout<<"\n\t Enter data of Book :\n";
B.getdata();
B.putdata();
return 0;
}
Output
Name - Arya Ranaware
Class - SECOMP D
Roll No. - 28
Experiment No : 4
Write a C++ program that creates an output file, writes information to it, closes the file and open
it again as an input file and read the information from the file. */
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char Give_Info[200];
ofstream Out_file; //provides output operations
Out_file.open("Information.bat", ios::out); //open for output operations
cout<<"\n\t Enter information to store it in file.(Not more than 200 characters)\n";
fgets(Give_Info,200,stdin);
Out_file<<Give_Info;
Out_file.close();
char Get_Info[200];
ifstream In_file; //provides input operations
In_file.open("Information.bat", ios::in); //open for input operations
In_file.getline(Get_Info, 200); //read next string from file
cout<<"\n\n\t Given Information is :\n";
cout<<Get_Info;
In_file.close();
return 0;