You are on page 1of 15

Name:Rupesh Budhathoki(191721)

/*
1. Write base class that ask the user to enter a complex 
number 
and derived class adds the complex number of its own with 
the base. 
Finally make third class that is friend of derived and ca
lculate 
the difference of base complex number and its own complex 
number.
*/

#include<iostream>
using namespace std;

  void display(int real,int img){
    if(img>0){
      cout<<real<<" +i"<<img;
    }else if(img<0){
      cout<<real<<" -i"<<-(img);
    }else{
      cout<<real;
    }
  }

class Complex{
  protected:
    int real,img;
  public:
    void input(){
      cout<<" Enter Complex number ";
      cin>>real>>img;
    }
};

class addComplex:public Complex{
  int areal,aimg;
  int sum_real,sum_img;
  public:
    void inputNew(){
      cout<<" Enter Complex number ";
      cin>>areal>>aimg;
    }
    void add(){
      sum_real = real + areal;
      sum_img = img + aimg;
      cout<<" Sum = ";
      display(sum_real,sum_img);
    }

  friend class diffComplex;
};

class diffComplex{
  int dreal,dimg;
  int diff_real,diff_img;
  public:
    void inputNewAgain(){
      cout<<" Enter Complex number ";
      cin>>dreal>>dimg;
    }
    void sub(addComplex C){
      diff_real = C.real - dreal;
      diff_img = C.img - dimg;
      cout<<" Difference = ";
      display(diff_real,diff_img);
    }
};

int main(){
  addComplex C1;
  diffComplex C2;
  cout<<" Addintion "<<endl<<"------------"<<endl;
  C1.input();
  C1.inputNew();
  C1.add();

  cout<<endl<<" Difference"<<endl<<"------------"<<endl;
  C2.inputNewAgain();
  C2.sub(C1);
}

/*
2. Define class Account to represent bank 
account of customer, derive classes Savings, 
FixedDeposit and Current from class Account. 
Next derive classes ShortTerm and LongTerm from 
class FixedDeposit. Write a program to 
implement this hierarchical relationship using 
parameterized constructor in each class.
*/
#include<iostream>
#include<string.h>
using namespace std;
class Account{
  protected:
  int Acc_no;
  char name[20],location[20];

  public:
    Account(){}
    Account(int a,char n[20],char l[20]){
      Acc_no = a;
      strcpy(name,n);
      strcpy(location,l);
    }
};

class Savings:public Account{
  int saving;
  public:
    Savings(){}
    Savings(int s,int a,char n[20],char l[20]):Account(a,n,
l){
      saving = s;
    }
};

class FixedDeposit:public Account{
  protected:
    int fd;
  public:
    FixedDeposit(){}
    FixedDeposit(int fd,int a,char n[20],char l[20]):Accoun
t(a, n, l){
      this->fd = fd;
    }
};

class Current:public Account{
  int current;
  public:
    Current(){}
    Current(int c,int a,char n[20],char l[20]):Account(a,n,
l){
      current = c;
    }
};

class ShortTerm:public FixedDeposit{
  public:
  ShortTerm(){}
  ShortTerm(int fd,int a,char n[20],char l[20]):FixedDeposi
t(fd,a,n,l){
    cout<<" Short Term Fixed deposit added";
  }
};

class LongTerm:public FixedDeposit{
  public:
  LongTerm(){}
  LongTerm(int fd,int a,char n[20],char l[20]):FixedDeposit
(fd,a,n,l){
    cout<<" Long Term Fixed deposit added";
  }
};

int main(){
  ShortTerm S(9,2,"ram","nepal");
  LongTerm L(10,4,"sam","us");
}

/*
3. Define a class Rectangle with data members 
to represent length and breadth and member 
function to input and calculate the area. 
Derive class Box with appropriate data member 
to represent height and member function to 
calculate surface area.
Use same function name input() and calculate() 
in both the classes and implement function 
overriding.
*/
#include<iostream>
using namespace std;

class Rectangle{
  protected:
    int l,b;
  public:
    void input(){
      cout<<"Enter l and b ";
      cin>>l>>b;
    }
    void calculate(){
      cout<<" Area = "<<l*b;
    }
};
class Box:public Rectangle{
  int h;
  public:
    void input(){
      cout<<" Enter h ";
      cin>>h;
    }
    void calculate(){
      cout<<" volume = "<<l*b*h;
    }
};

int main(){
  Box B;
  B.Rectangle::input();
  B.Rectangle::calculate();
  
  B.input();
  B.calculate();
}

/*
4. Write a program using virtual destructor.
*/
#include<iostream>
  
using namespace std;
  
class base {
  public:
    base(){ 
      cout<<"Constructing base \n"; 
    }
    virtual ~base(){ 
      cout<<"Destructing base \n"; 
    }     
};
  
class derived: public base {
  public:
    derived() {
       cout<<"Constructing derived \n"; 
    }
    ~derived(){ 
      cout<<"Destructing derived \n"; 
    }
};

int main()
{
  derived *d = new derived();  
  base *b = d;
  delete b;
  getchar();
  return 0;
}

/*
5. Define a class Student, derive classes 
AcademicMarks and PracticalMarks from class Student. 
Next derive class Result from AcademicMarks and PracticalM
arks. 
And calculate the result of student. Use parameterized co
nstructor 
in each class to set the initial values of data.
*/

#include<iostream>
#include<string.h>
using namespace std;

class Student{
  protected:
  int RN;
  char name[20];
  public:
    Student(){}
    Student(int r,char n[]){
      RN = r;
      strcpy(name,n);
    }
};

class AcademicMarks:public Student{
  protected:
  int math,cpp;
  
  public:
    AcademicMarks(){}
    AcademicMarks(int m,int c,int r,char n[]):Student(r,n){
      math = m;
      cpp = c;
    }
};
class PracticalMarks:public Student{
  protected:
  int football,quiz;
  public:
    PracticalMarks(){}
    PracticalMarks(int f,int q,int r,char n[]):Student(r,n)
{
      football = f;
      quiz = q;
    }
};

class Result:public AcademicMarks,PracticalMarks{
  int total;
  public:
    Result(){}
    Result(int m,int c,int f,int q,int r,char n[]):Academic
Marks(m,c,r,n),PracticalMarks( f, q, r, n){
      total = m+c+f+q;
      cout<<"Total = "<<total;
    }
};

int main(){
  Result R(90,90,50,70,21,"sam");
}

/*
6. A book shop sells books and video tapes. Define abstra
ct 
class known as Media to store title and price of a public
ation, 
create two derived classes, one to store number of Book i
n a 
book and another class to store playing Tape of tape. Fun
ction 
display() is used in all classes to display the contents. 
Create 
constructor to set the initial values. Display information 
of book 
and tape using base class’s pointer.
*/

#include<iostream>                                          
                                                            
        
#include<string.h>
using namespace std;

class Media{
  protected:
  char title[20];
  int price;

  public:
    Media(){}
    Media(char t[],int p){
      strcpy(title,t);
      price = p;
    }
    virtual void display(){
      cout<<" Title = "<<title<<" Price = "<<price;
    }
};

class Book:public Media{
  int totalPage;
  public:
    Book(){}
    Book(int tp,char t[],int p):Media(t,p){
      totalPage = tp;
    }
    void display(){
      cout<<" Total Pages = "<<totalPage;
    }
};

class Tape:public Media{
  int playTime;
  public:
    Tape(){}
    Tape(int pt,char t[],int p):Media(t,p){
      playTime = pt;
    }
    void display(){
      cout<<" Total Tape Played = "<<playTime;
    }
};

int main(){
  Media *M = new Media;
  Book B(100,"Sadvab",200);
  Tape T(10,"Humanity",300);
  B.Media::display();
  M = &B;
  M->display();

  T.Media::display();
  M = &T;
  M->display();
}

/*
7. Define a class Date which has data members to represen
t month, 
day and year. Define a class Address with data members to 
represent 
country, province numbers, state, postal code, municipalit
y and ward 
number. Define a class Student which has attributes respe
ctive to 
student and has birthdate and address. Write a program to 
implement containership.
*/
#include<iostream>
using namespace std;

class Date{
  int m,d,y;
  public:
    void input(){
      cout<<" Enter date of birth ";
      cin>>d>>m>>y;
    }              
    void display(){
      cout<<" Date of birth : "<<d<<":"<<m<<":"<<y;
    }               
};

class Address{
  char country[20],state,municipality;
  int province_num,postal_code,ward_num;
  public:
    void input(){
      cout<<" Enter country, state, municipality, province 
number, postal code  and ward number";
      cin>>country>>state>>municipality>>province_num>>post
al_code>>ward_num;
    }
    void display(){
      cout<<" Country = "<<country
          <<" State = "<<state
          <<" Municipality = "<<municipality
          <<" Province Number = "<<province_num
          <<" Postal Code = "<<postal_code
          <<" Ward Number = "<<ward_num;
    }
};

class Student{
  int roll_no;
  char name[20];
  Date D;
  Address A;
  public:
    void input(){
      cout<<" Enter name , rollno ";
      cin>>name>>roll_no;
      D.input();
      A.input();
    }

    void display(){
      cout<<" Name = "<<name<<" Roll no = "<<roll_no;
      D.display();
      A.display();
    }
};

int main(){
  Student S;
  S.input();
  S.display();
}

You might also like