SE Computer SPPU Object Oriented Programming (OOP) all codes and output (2019 pattern)

 

Created By – Learning Bandhu Youtube

Q1. Implement a class complex which represents the complex number datatypes.Implement the following operations:

1.Constructor(including a default constructor which creates the complex number 0+0i);

2.Overload operator + to add two complex numbers.

3.Overload operator * to multiply two complex numbers.

4. Overload operator << and >> to print and read complex numbers.

Code:

#include<iostream>
using namespace std;
class Complex
{
 public :
float real,img;
Complex()
{}
Complex operator +(Complex);
Complex operator *(Complex);
friend ostream & operator <<(ostream&,Complex&);
friend istream & operator >>(istream&,Complex&);
};

Complex Complex::operator +(Complex obj)
{
Complex temp;
temp.real=real+obj.real;
temp.img=img+obj.img;
return temp;
}

Complex Complex::operator *(Complex obj)
{
Complex temp;
temp.real=real*obj.real-img*obj.img;
temp.img=real*obj.img+img*obj.real;
return temp;
}

//definition of istream
istream& operator >>(istream& is,Complex& obj)
{
 is>>obj.real;
is>>obj.img;
return is;
}
//definition of ostream
ostream& operator <<(ostream& os,Complex& obj)
{
os<<obj.real;
os<<"+"<<obj.img<<"i";
return os;
}
int main()
{
Complex a,b,c,d,e;
cout<<endl<<"enter 1st no"<<endl;
cout<<"enter real and img. no = "<<endl;
cin>>a;
cout<<endl<<"enter 2nd no"<<endl;
cout<<"enter real and img. no = "<<endl;
cin>>b;
cout<<endl<<"arithmatic operations are"<<endl;
c=a+b;
cout<<"addition is= "<<c<<endl;
d=a*b;
cout<<"multiplication is ="<<d<<endl<<endl;


return 0;
}

Output:

 

 

 

 

Q2. Develop a program in C++ to create a database of student’s 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. Make use of constructor, default constructor, copy constructor,

destructor, static member functions, friend class, this pointer, inline code and dynamic

memory allocation operators-new and delete as well as exception handling.

 

Code:

#include<iostream>

#include<string>

#include<cstring>

#include<stdlib.h>

 

using namespace std;

class student

{  int roll;

 char name[30];

float marks;

public:

 

student()

{  roll=0;

   marks=0;

   strcpy(name,"");

}

 student(int roll,char name[30], float marks)

{

   this->roll=roll;

   this->marks=marks;

 strcpy(this->name,name);

}

void accept()

{   cout<<"Enter the name";

    cin>>name;

    cout<<"Enter the rollno";

    cin>>roll;

    cout<<"Enter the marks";

    cin>>marks;

}

void display()

{    cout<<"\n\nNAME:"<<name;

  cout<<"\n\nROLLNO:"<<roll;

  cout<<"\n\nMARKS:"<<marks;

}

};

 

 

int main()

{  

    int i,ch,n;

   student s[10];

 

 

while(1)

{

cout<<"\n1.CREATE\n2.DISPLAY\n3.EXIT";

cout<<"\n Enter your choice ";

cin>>ch;

 

switch(ch)

{   case 1:

           cout<<"\n Enter how many student information you want to create";

           cin>>n;

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

    {   cout<<"enter information of"<<i+1<<"th student";

     s[i].accept();

   

    }

    break;

  case 2:

    cout<<"Student database is as follows";

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

        s[i].display();

        break;

  case 3:  exit(0);

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

}

   }return 0;

Output:

 

 

 

Q3. Imagine a publishing company which does marketing for book and audio cassette versions. Create a class

publication that stores the title (a string) and price (type float) of publications. 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.

 

Code:

#include <iostream>

using namespace std;

class publication

{   public:

     float price;

     string name;

     publication()

     {

          name="none";

          price=0.0;

     }

     void getdata(string s)

     {

         cout<<"\n Enter the name of the"<<s<<":";

         cin>>name;

         bool flag=false;

         while(flag==false)

         {

             cout<<"\n Enter the price of the"<<s<<":";

             cin>>price;

           

            try

            {

                if(price<=0)

                throw price;

                else

                flag=true;

               

            }

            catch(float x)

            {

                price=0.0;

                cout<<"\n You have entered price is invalid ";

                cout<<"\n The price has set to "<<price;

                cout<<"\n Enter valid input";

            }

         }

        

     }

   

inline string getname()

{

    return name;

}

inline float getprice()

{

    return price;  

}

 

};

class book: public publication

{

   public:

  int pages;

  book()

  {

      pages=0;

  }

  void getpages()

     {

        

         bool flag=false;

         while(flag==false)

         {

             cout<<"\n Enter the no of pages";

             cin>>pages;

           

            try

            {

                if(pages<=0)

                throw pages;

                else

                flag=true;

               

            }

            catch(int x)

            {

                pages=0;

                cout<<"\n You have entered no of pages  is invalid ";

                cout<<"\n The no of pages  has set to "<<pages;

                cout<<"\n Enter valid input";

            }

         }

        

     }

     void display()

     { cout<<"\t\n book is Found";

       cout<<"\t\n Details of book";

       cout<<"\t\n Name of the Book"<<getname();

       cout<<"\t\n No of pages"<<pages;

       cout<<"\t\n The price is"<<getprice();

        

     }

    

 

};

class tape: public publication

{

  public:

  float time;

  tape()

  {

      time=0.0;

  }

  void gettime()

     {

       

         bool flag=false;

         while(flag==false)

         {

            cout<<"\n Enter the playing time of tape in mins";

            cin>>time;

           

            try

            {

                if(time<=0)

                throw time;

                else

                flag=true;

               

            }

            catch(float x)

            {

                time=0.0;

                cout<<"\n You have entered time is invalid ";

                cout<<"\n The time has set to "<<time;

                cout<<"\n Enter valid input";

            }

         }

        

     }

void display()

     { cout<<"\t\n Audio cassette is Found";

       cout<<"\t\n Details of Audio cassette";

       cout<<"\t\n Name of the Tape "<<getname();

       cout<<"\t\n Playing time"<<time<<"Mins";

       cout<<"\t\n The price is"<<getprice();

        

     }

   

};

int main()

{

  int choice;

  string n;

  book objb;

  tape objt;

  cout<<"\t\n  Welcome to inheritance Program";

  do

  {

       cout<<"\n Menu";

       cout<<"\t1.Insert a book\n\t2.Insert an audio cassette\n\t3.Display Book Details";

       cout<<"\n\t4.Display audio cassttee details\n\t5.Exit";

       cout<<"\t\n Enter the choice";

       cin>>choice;

       switch(choice)

       {    case 1:  objb.getdata("book");

                     objb.getpages();

                     cout<<"\t\n book is being searched"<<endl;

                     break;

            case 2: objt.getdata("Audio Cassttee");

                    objt.gettime();

                    cout<<"\t\n Audio Cassttee is being searched"<<endl;

                    break;

            case 3: try

                    {  

                        if(objb.getname()=="NONE")

                           throw 3;

                        else

                        {

                          objb.display();

                          cout<<"\t\n We will hope that you purchase book again";

                        }

                    }

                    catch(int x)

                    {

                      cout<<"\t\n No book has been searched yet";

                      cout<<"\t\n choose correct option";

                    }

                    break;

            case 4: try

                    {  

                        if(objt.getname()=="NONE")

                           throw 3;

                        else

                        {

                          objt.display();

                          cout<<"\t\n We will hope that you purchase audio Cassttee again";

                        }

                    }

                    catch(int x)

                    {

                      cout<<"\t\n No audio cassette has been searched yet";

                      cout<<"\t\n choose correct option";

                    }

                    break;

            case 5: cout<<"\t\n ..........VISIT AGAIN..........";

                    break;

       

            }

      

     

    }while(choice!=5);

  cout<<"\t\n End of the program";

  cout<<"\t\n Thnak you";

 

    return 0;

}

 

Output:

Q4. Write a C++ program that creates an output file, writes information to it, closes the file, open

it again as an input file and read the information from the file.

 

Code:

#include <iostream>

#include <fstream>

 

using namespace std;

 

class employee

{

char name[20];

int emp_id;

float salary;

public:

void accept()

{

cin>>name;

cin>>emp_id;

cin>>salary;

}

 

 

void display()

{

cout<<"\n"<<name<<"\t"<<emp_id<<"\t"<<salary;

}

};

 

 

int main()

{

employee o[5];  // employee o; array of object

fstream f;

int i,n;

 

 

f.open("input.txt"); //create employee

cout<<"\n How many employee information wanted to store:";

 

 

 

 

cin>>n;

cout<<"\n Enter information of 3 employees (Enter name, emp_id, salary)";

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

{

cout<<"\n Enter information of "<<i<<" employee";

o[i].accept(); //accept input from user

f.write((char *)&o[i], sizeof(o[i])); //write object in employee

}

f.close();

 

 

f.open("input.txt", ios::in);

cout<<"\n Information of employee is as follows";

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

{

f.read((char*)&o[i], sizeof(o[i])); //read data from employee

o[i].display();

}

f.close();

 

 

return 0;

 

 

}

 

 

 

Output:

 

 

 

 

 

 

Q5. Write a function template for selection sort that inputs,sorts and outputs an integer array

and a float array.

 

Code:

#include<iostream>

#define size 10

using namespace std;

int n;

template<class T>

void selection(T A[size])

{

   int i,j,min;

    T temp;

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

    {  min=i;

       for(j=i+1;j<n;j++)

         {   if(A[j]<A[min])

                min=j;

         }

        

          temp=A[i];

          A[i]=A[min];

          A[min]=temp;

    }

    cout<<"\n The sorted list is:...";

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

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

}

int main()

{  

    int i,A[size];

    float B[size];

cout<<"Selection Sort";

cout<<"\nHandling Interger Elements";

cout<<"\nHow Many Elements Are There???";

cin>>n;

cout<<"\nEnter the integer numbers:-";

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

  cin>>A[i];

 selection(A);

cout<<"\nHandling Float Elements";

cout<<"\nHow Many Elements Are There???";

cin>>n;

cout<<"\nEnter the Float numbers";

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

  cin>>B[i];

selection(B);

cout<<"\n";

return 0;

}

OUTPUT:

Q6. Write C++ program using STL for sorting and searching with user defined records such as

person record(Name, DOB, Telephone number), Item record (Item code, name,

cost,quantity) using vector container.

 

Code:

#include <iostream>

#include <vector>

#include <string>

#include <algorithm>

 

using namespace std;

 

class Record{

public:

               int item_code, quantity;

               float cost;

               string name;

              

               bool operator == (const Record &o1){

                              if (name == o1.name)

                                             return 1;

                              return 0;

               }

              

               bool operator < (const Record & o1){

                              if(item_code < o1.item_code)

                                             return 1;

                              return 0;

               }

};                          

               vector <Record>r1;

               void insert();

               void display();

               void print1(Record &r1);

               void search();

               void del();

              

               void insert(){

                              Record o1;

                              cout<<"\nEnter name: ";    cin>>o1.name;

                              cout<<"\nEnter item code: ";      cin>>o1.item_code;

                              cout<<"\nEnter required quantity: ";       cin>>o1.quantity;

                              cout<<"\nEnter cost: ";    cin>>o1.cost;

                             

                              r1.push_back(o1);                          

               }

              

               void display(){

                              for_each(r1.begin(), r1.end(), print1);

               }

              

               void print1(Record &r1){

                              cout<<"\nName is: "<<r1.name<<endl;

                              cout<<"Item_code is: "<<r1.item_code<<endl;

                              cout<<"Quantity: "<<r1.quantity<<endl;

                              cout<<"Cost: "<<r1.cost<<endl;                

               }

              

               void search(){

                             

                              Record o1;

                              vector <Record>::iterator i;

                             

                              cout<<"\nEnter name to be searched: ";    cin>>o1.name;

                             

                              i=find(r1.begin(),r1.end(),o1);

                                             if(i==r1.end()){

                                                            cout<<"\n Not found";

                                             }

                                             else{

                                                            cout<<"\n Record found";

                                             }

                              }

                                             bool compare(const Record &r1,const Record &r2)

                             

                                             {

                                                            return r1.item_code<r2.item_code;

               }

              

               void del(){

                                             vector<Record>::iterator i;

                                             Record o1;

                                             cout<<"\n Enter name of record to be delete: ";

                                             cin>>o1.name;

                                            

                                             i=find(r1.begin(),r1.end(),o1);

                                            

                                             if(i==r1.end()){

                                                            cout<<"\n Not found";

                                             }

                                            

                                             else{

                                                            r1.erase(i);

                                                            cout<<"\n Deleted";

                                             }

                              }

 

               int main(){

               int ch;

              

   do{

                              cout<<"\n...MENU...";

                              cout<<"\n 1.INSERT";

                              cout<<"\n 2.DISPLAY";

                              cout<<"\n 3.SEARCH";

                              cout<<"\n 4.SORT";

                              cout<<"\n 5.DELETE";

                              cout<<"\n 6.EXIT";

   

   

               cout<<"\n ENTER YOUR CHOICE:- ";

               cin>>ch;

   

               switch(ch){

                              case 1: cout<<"\n Enter datails of item: ";

                                   insert();

                                   break;

                              case 2: cout<<"\n Display details of item: ";

                                                  display();

                                                             break;

                                             case 3:

                                                  search();

                                                             break;

                                             case 4:

                                             sort(r1.begin(),r1.end(),compare);

                                             cout<<"\n Sorted order on roll number";

                                             display();

                                                            break;

                                             case 5:

                                             del();

                                                            break;                                       

                              case 6: cout<<"\n Wrong choice"              ;

                              break;

                              }

               }while(ch!=6);

              

               return 0;

}

Output:

 

 

 

 

 

 

 

 

Q7. Write a program in C++ to use map associative container. The keys will be the names of     states and the values will be the populations of the states. When the program runs, the user is prompted to type the name of a state. The program then looks in the map, using the state name as an index and returns the population of the state.

 

Code:

#include<iostream>

#include<map>

#include<string>

using namespace std;

int main()

{

  string state;

  float population;

  char ans='y';

  int choice;

 

 map<string,float> m;

 map<string,float>::iterator i;

 

 do

 {

   cout<<"---MAIN MENU--"<<endl;

   cout<<"\n1.INSERT STATE:-";

   cout<<"\n2.DISPLAY";

   cout<<"\n3.SEARCH STATE";

   cout<<"\n4.DELETE"<<endl;

 

   cout<<"ENTER YOUR CHOICE:- "<<endl;

   cin>>choice;

   switch(choice)

   {

     case 1:

       cout<<"Enter name of state:";

       cin>>state;

       cout<<"\n Enter the population(Cr):";

       cin>>population;

       m.insert(pair<string,float>(state, population));

       break;

   

     case 2:

       cout<<"State and population are:"<<endl;

       for(i=m.begin();i!=m.end();i++)

         cout<<"["<<(*i).first<<","<<(*i). second<<"]";

       break;

   

       cout<<"Enter state name for searching population:"<<endl;

       cin>>state;

       if(m.count(state)!=0)

         cout<<"\n Population is"<<m.find(state)->second<<"Cr";

       else

         cout<<"The state is not present!"<<endl;

       break;

   

     case 4:

      cout<<"Enter the state to be deleted:"<<endl;

      cin>>state;

      m.erase(state);

      cout<<"The state is deleted"<<endl;

      break;

    }

    cout<<"\n`Do you want to continue?(y/n)"<<endl;

    cin>>ans;

    } while(ans=='y');

    return 0;

}

 

Output:

 Thank from Learning Bandhu

 

 

Comments

  1. Subscribe to Learning Bandhu on Youtube and help us to reach 1k

    ReplyDelete

Post a Comment

Popular Posts