Oop Practical
Oop Practical
Oop Practical
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()
{
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;
}
/*
student@student-OptiPlex-3010:~$ ./a.out
Default constructor value=
0+0i
/*--------------------------OUTPUT--------------------------------------
gescoe@gescoe-OptiPlex-3010:~/Desktop/se$ g++ fifth.cpp
gescoe@gescoe-OptiPlex-3010:~/Desktop/se$ ./a.out
Initial Size: 0
Enter The 5 Element: 6 9 4 2 1
-----------------------------------------------------------------------*/
/*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. 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.h>
using namespace std;
class person_additional_info
{
char address[20],license[20],insurance[20];
long int contact;
public:
person_additional_info() //Default constructor
{
strcpy(address,"XYZ");
strcpy(license,"XY-0000000000");
strcpy(insurance,"XY00000000X");
contact=000000000;
~person_additional_info() //Destructor
{
cout<<"I am in Destructor";
}
public:
person() //Default constructor
{ strcpy(name,"XYZ");
strcpy(dob,"dd/mm/yy");
strcpy(blood,"A +");
ht=0;
wt=0;
pai=new person_additional_info;
}
person(person*p1) //Copy constructor
{ strcpy(name,p1->name);
strcpy(dob,p1->dob);
strcpy(blood,p1->blood);
ht=p1->ht;
wt=p1->wt;
pai=new person_additional_info;
strcpy(pai->address,p1->pai->address);
strcpy(pai->license,p1->pai->license);
strcpy(pai->insurance,p1->pai->insurance);
pai->contact=p1->pai->contact;
}
static void showcount() //Static member function
{
cout<<"\nNo of records count="<<count<<"\n";
}
~person() //Destructor
{
cout<<"\nI am in Destructor\n";
}
void getdata(char name[20]);
inline void display(); // Inline function declaration
};
void person::getdata(char name[20])
{
strcpy(this->name,name); //this pointer
cout<<"\n Enter date of birth";
cin>>dob;
cout<<"\n Enter blood group";
cin>>blood;
cout<<"\n Enter height";
cin>>ht;
cout<<"\n Enter weight";
cin>>wt;
cout<<"\n Enter address";
cin>>pai->address;
cout<<"\n Enter Licence number";
cin>>pai->license;
cout<<"\n Enter Insurance policy number";
cin>>pai->insurance;
cout<<"\n Enter Contact number";
cin>>pai->contact;
count++;
}
//inline function definition
void person::display()
{
cout<<"\t"<<name;
cout<<"\t"<<dob;
cout<<"\t"<<blood;
cout<<"\t"<<ht;
cout<<"\t"<<wt;
cout<<"\t"<<pai->address;
cout<<"\t"<<pai->license;
cout<<"\t"<<pai->insurance;
cout<<"\t"<<pai->contact;
}
int main()
{
person *p1,*p2;
int ch;
p1=new person; //call default constructor & dynamic memory allocation
p2=new person(p1); //call copy constructor
cout<<"\tName";
cout<<"\tDob";
cout<<"\t Blood";
cout<<"\tHt";
cout<<"\tWt";
cout<<"\tAddress";
cout<<"\tLicense";
cout<<"\tInsurance";
cout<<"\tContact";
cout<<endl;
cout<<"Default Constructor Value \n";
p1->display();
cout<<"\n";
cout<<"Copy Constructor Value \n";
p2->display();
int n;
cout<<"\nEnter how many records you want??";
cin>>n;
person p3[n]; //array of object
char name[20];
int x=0;
do
{
cout<<"\nWelcome to Personal database system";
cout<<"\n1.Enter the record";
cout<<"\n2.Display the record";
cout<<"\n3.Exit";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\nEnter the Name ";
cin>>name;
p3[x].getdata(name);
person::showcount(); // calls static function
x++;
break;
}
case 2:
{
cout<<"\tName";
cout<<"\tDob";
cout<<"\t Blood";
cout<<"\tHt";
cout<<"\tWt";
cout<<"\tAddress";
cout<<"\tLicense";
cout<<"\tInsurance";
cout<<"\tContact";
for(int i=0;i<n;i++)
{
cout<<"\n";
p3[i].display(); //calls inline function
}
break;
}
}
}while(ch!=3);
delete p1; //dynamic memory de-allocation
delete p2;
return 0;
Enter height5
Enter weight50
Enter addresspune
No of records count=1
I am in Destructor
I am in Destructor
I am in Destructor
I am in Destructor
*/
/*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;
class Calculator
{
private:
float num1,num2,result;
char op;
public:
void get();
void calculate();
};
void Calculator::get()
{
cout<<"\nEnter first number, operator, second number:\n";
cin>>num1;
cin>>op;
cin>>num2;
}
void Calculator::calculate()
{
switch(op)
{
case '+':
result=num1+num2;
cout<<" Answer = "<<result;
break;
case '-':
result=num1-num2;
cout<<" Answer = "<<result;
break;
case '*':
result=num1*num2;
cout<<" Answer = "<<result;
break;
case '/':
if(num2==0)
cout<<"\n Error. Not valid.";
result=num1/num2;
cout<<" Answer = "<<result;
break;
}
}
int main()
{
char ag;
Calculator obj;
x:obj.get();
obj.calculate();
cout<<"\n Do another (y/n)? ";
cin>>ag;
if(ag=='y'||ag=='Y')
goto x;
return 0;
}
/*OUTPUT:
student@student-OptiPlex-3010:~$ g++ groupa5.cpp
student@student-OptiPlex-3010:~$ ./a.out
*/
#include<iostream>
using namespace std;
class personal
{
protected:
char name[50];
char address[50];
char birthdate[50];
char gender;
public:
void get_personal()
{
cout<<"\nEnter name";
cin>>name;
cout<<"\nEnter Address";
cin>>address;
cout<<"\nEnter Birthdate(dd/mm/yyyy)";
cin>>birthdate;
cout<<"\nEnter gender(M/F)";
cin>>gender;
}
};
class professional
{
protected:
int expinnoofyear;
char orgname[50];
char projname[50];
char projdetails[50];
public:
void get_professional()
{
cout<<"\nEnter number of years of exp";
cin>>expinnoofyear;
cout<<"\nEnter organization name";
cin>>orgname;
cout<<"\nEnter project name";
cin>>projname;
cout<<"\nEnter project Details";
cin>>projdetails;
}
};
class academic
{
protected:
int year;
int marks;
int percentage;
char clas[50];
public:
void get_academic()
{
cout<<"\nEnter academic year";
cin>>year;
cout<<"\nEnter total marks";
cin>>marks;
cout<<"\nEnter percentage";
cin>>percentage;
cout<<"\nEnter class";
cin>>clas;
}
};
class biodata: public personal,public academic,public professional
{
public:
void display()
{
cout<<"\n--------------Employee Biodata------------"<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"Personal Details"<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;
cout<<"Birthdate:"<<birthdate<<endl;
cout<<"Gender:"<<gender<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"----------------Academic Details--------------"<<endl;
cout<<"Academic
Year\t"<<"marks\t"<<"percentage\t"<<"class\t"<<endl;
cout<<year<<"\t"<<marks<<"\t"<<percentage<<"\t"<<clas<<endl;
cout<<"-------------------------------------------------------"<<endl;
cout<<"-----------------Professional
Details--------------------"<<endl;
cout<<"Organization Name:"<<orgname<<endl;
cout<<"Years of Experince:"<<expinnoofyear<<endl;
cout<<"Project Done:"<<projname<<endl;
cout<<"Project Details:"<<projdetails<<endl;
}
};
int main()
{
biodata b;
b.get_personal();
b.get_academic();
b.get_professional();
b.display();
return 0;
}
/*student@student-OptiPlex-3010:~$ g++ groupa13.cpp
student@student-OptiPlex-3010:~$ ./a.out
Enter nameXYZ
Enter AddressPUNE
Enter Birthdate(dd/mm/yyyy)15/5/2016
Enter gender(M/F)F
Enter percentage70
Enter classFirst
--------------Employee Biodata------------
-------------------------------------------------------
Personal Details
Name:XYZ
Address:PUNE
Birthdate:15/5/2016
Gender:F
-------------------------------------------------------
----------------Academic Details--------------
Academic Year marks percentage class
2016 500 70 First
-------------------------------------------------------
-----------------Professional Details--------------------
Organization Name:PQR
Years of Experince:5
Project Done:PPP
Project Details:III
student@student-OptiPlex-3010:~$
*/
/*Write C++ Program with base class convert declares two variables, val1 and
val2,
which hold the initial and converted values, respectively. It also defines the
functions
getinit( ) and getconv( ), which return the initial value and the converted
value.
These elements of convert are fixed and applicable to all derived classes that
will inherit convert. H
owever, the function that will actually perform the conversion, compute( ),
is a pure virtual function that must be defined by the classes derived from
convert.
The specific nature of compute( ) will be determined by what type of conversion
is taking place.*/
#include <iostream>
class convert {
protected:
double val1;
double val2;
public:
convert(double i){
val1=i;
}
double getconv() {return val2;}
double getinit() {return val1;}
virtual void compute ()=0;
};
//Liters to gallons
class l_to_g:public convert
{
public:
l_to_g(double i):convert(i){}
void compute()
{
val2=val1/3.7854;
}
};
/***********output************