Infolinks - Earn Easy Money

Semester 2 Assignments

Pre - Mid Assignment:

Library Management System

In this assignment, we uses some basic techniques of C++ programming using objects , classes and Functions.
Source Code:


#include<iostream>
#include<conio.h>
using namespace std;
class Books
{
     string *books;
     int no,*B_Pages,*B_Price;
     string *B_Name,*A_Name,*B_Edition;
     public:
     Books (int r)
     {
        no=r;
        books=new string[r];
     }
     void Input (void)
     {
        for(int i=0; i<no ; i++)
        {
           cout<<"\t::Enter the Following things of book #"<<i+1<<"::";   
           cout<<"\nEnter the Serial No. of book from 1--5::";
           cin>>books[i];
           cout<<"\nEnter the Name of book::";
           cin>>*B_Name;
           cout<<"\nEnter the Name of Author::";
           cin>>*A_Name;
           cout<<"\nEnter the Edition of book::";
           cin>>*B_Edition;
           cout<<"\nEnter the Pages of book::";
           cin>>*B_Pages;
           cout<<"\nEnter the Price of book::";
           cin>>*B_Price;
        }
     }
     void Display (void)
     {
        for(int i=0; i<no ; i++)
        {
           cout<<"\n\t::Information of book #"<<i+1<<"::"; 
           cout<<"\nSerial No. of book is::"<<books[i];   
           cout<<"\nName of book is ::"<<*B_Name;
           cout<<"\nAuthor of the book is ::"<<*A_Name;
           cout<<"\nEdition of book is ::"<<*B_Edition;
           cout<<"\nPages of book are ::"<<*B_Pages;
           cout<<"\nPrice of book is::"<<*B_Price;
        }
     }
     void Search (void)
     {
         cout<<"\nEnter the book name for search :";
          cin>>*B_Name;
          for(int i=0; i<no ; i++)
          {
            if(*B_Name==books[i])
             {
                 cout<<"Name of book is ::"<<*B_Name;
                 cout<<"\nAuthor of the book is ::"<<*A_Name;
                 cout<<"\nEdition of book is ::"<<*B_Edition;
                 cout<<"\nPages of book are ::"<<*B_Pages;
                 cout<<"\nPrice of book is::"<<*B_Price;
             }
          }
     }
};
int main ()
{
    int r;
    cout<<"\nEnter the number of books::";
    cin>>r;
    Books B(r);
    B.Input();
    B.Display();
    B.Search();
    return 0;
}
         
        ___________________________________________________________

Post - Mid Assignment:

Inheritance Assignment

In this assignment, we uses inheritance techniques of C++ programming using objects , classes and Functions.
Source Code:

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

class Person
{
      protected:
                string name,fname,id;
                
      public:
 Person()
 {
 }
             Person(string n,string fn,string i)
             {
                name=n;
                fname=fn;
                id=i;
             }
        void display_p()
        {     
     cout<<"\t Case no 1 \n";
              cout<<"Name:"<<name<<endl<<"Father name:"<<fname<<endl<<"ID CARD NO:"<<id<<endl;
        }
         void setname(string n)
         {
             name=n;
         }
         string getname()
         {
                return name;
         }
         void setfname(string fn)
         {
              fname=fn;
         }
         string getfname()
         {
                return fname;
         }
         void setid(string i)
         {
              id=i;
         }
         string getid()
         {
                return id;
         }
};
         
class Student:public Person
{
      protected:
string ID,Class,program;
      public:
 Student()
{
 }
             Student(string n, string fn,string i,string I,string C,string P)

             {
                name=n;
                fname=fn;
                id=i;
                ID=I;
                Class=C;
                program=P;
             }
      void display_s()
      {
         cout<<"\t Case no 2 \n";
cout<<"Name:"<<name<<endl<<"Father Name:"<<fname<<endl<<"ID CARD NO:"<<id<<endl;
         cout<<"STUDENT ID:"<<ID<<endl<<"STUDENT CLASS:"<<Class<<endl;
cout<<"STUDENT PROGRAM:"<<program<<endl ;
      }  
      void setname(string n)
         {
             name=n;
         }
         string getname()
         {
                return name;
         }
         void setfname(string fn)
         {
              fname=fn;
         }
         string getfname()
         {
                return fname;
         }
         void setid(string i)
         {
              id=i;
         }
         string getid()
         {
                return id;
         }
            void setID(string ID)
         {
             id=ID;
         }
         string getID()
         {
                return ID;
         }
         void setClass(string C)
         {
              Class=C;
         }
         string getClass()
         {
                return Class;
         }
         void setProgram(string P)
         {
              program=P;
         }
         string getProgram()
         {
                return program;
         }
};

class Staff :public Person
{
      protected:
string empcode, department;
      public:
 Staff ()
{
 }
             Staff (string n, string fn,string i,string code,string dpt)

             {
                name=n;
                fname=fn;
                id=i;
                empcode=code;
                department=dpt;
             }
      void display_S()
        {
           cout<<"\t Case no 3 \n";
cout<<"Teacher Name:"<<name<<endl<<"Father Name:"<<fname<<endl<<"ID CARD NO:"<<id<<endl ;
           cout<<"Teacher emp code:"<<empcode<<endl<<"Teacher department:"<<department<<endl ;
        }  
      void setname(string n)
         {
             name=n;
         }
         string getname()
         {
                return name;
         }
         void setfname(string fn)
         {
              fname=fn;
         }
         string getfname()
         {
                return fname;
         }
         void setid (string i)
         {
              id=i;
         }
         string getid()
         {
               return id;
         }
         void setcode(string code)
         {
              empcode=code;
         }
         string getcode()
         {
                return empcode;
         }
         void setdpt(string dpt)
         {
             department=dpt;
         }
         string getdpt()
         {
                return department;
         }
};

class Teacher :public Staff
{
      protected:
string course, designation , experience;
      public:
 Teacher ()
{
 }
             Teacher (string code,string dpt, string cors , string dgn , string exp)

             {
                empcode=code;
                department=dpt;
                course=cors;
                designation=dgn;
                experience=exp;
             }
      void display_T()
      {
         cout<<"\t Case no 4 \n";
cout<<"Teacher emp code:"<<empcode<<endl<<"Department:"<<department<<endl<<"Course:"<<course<<endl;
         cout<<"Designation:"<<designation<<endl<<"Experience:"<<experience<<endl;
      }  
         void setcode(string code)
         {
             empcode=code;
         }
         string getcode()
         {
                return empcode;
         }
         void setdpt(string dpt)
         {
              department=dpt;
         }
         string getdpt()
         {
                return department;
         }
         void setcourse(string cors)
         {
              course=cors;
         }
         string getcourse()
         {
                return course;
         }
            void setdgn(string dgn)
         {
             designation=dgn;
         }
         string getdgn()
         {
                return designation;
         }
         void setexp(string exp)
         {
             experience=exp;
         }
         string getexp()
         {
                return experience;
         }
};
int main ()
{
        // Case I
 Person obj("M.Rehan Asghar","M.Asghar Ali","35201-6873690-5");
 obj.display_p();
 obj.setname("M.Usman Asghar") ;
 obj.display_p ();

           // Case II
 Student ob("Ahmed","Sharafat","3520168736905","15125","bsse 2","Software Engineering");
 ob.display_s();

           // Case III
 Staff S ("Sir Rehan","Ahmed Ali","3520168736905","298","CS and IT");
 S.display_S();

           // Case IV
 Teacher T ("297","Cs and IT","OOP","Head of Department","3.5 years");
 T.display_T();
 return 0;
}     

________________________________________    

OOP Final Lab Assignment:

Task1:                 
Define a base class shape with protected data members; width and height and setter member functions for setting width and height, also define the derived class rectangle publically inherited from class shape. Class rectangle should have a member function getarea() which should compute and return area of a rectangle. Declare an object of rectangle in main function and set its width and height then compute its area?

Program Code:
               
#include<iostream>
#include<conio.h>
using namespace std;
class shapes
{
      protected:
                int width,height;
public:
      void setter(int w ,int h)
{      
      width = w;
      height = h;
}
};
class rectangle:public shapes
{
      public:
     int area;
      int  getarea()
{    
      area = width*height;
      cout<<"Area = "<<area;
      return area;
}
};
int main()
{         
   class rectangle r;
   r.setter(8,7);
   r.getarea();
   return 0;
}

Task2:
(Modifying Task1) Define another class named paint_cost with a member function get_cost() which returns paint cost of the rectangle on the basis of its area; assume cost=area*100. Inherit class rectangle from class shape and paint_cost.

Program Code:
                
#include<iostream>
#include<conio.h>
using namespace std;
 class shapes
 {
       protected:
                 int width,height;
       public:
              void setwidth(int w)
 {
              width=w;
}
              void setheight(int h)
{
              height=h;
}
};
 class paint_cost
 {
       protected:
                 int area;
       public:
              int getcost(int a)
 {
     area = a;       
                 return area*100;
}
};
 class rectangle:public shapes,public paint_cost
 {
       public:
              int getarea()
{
              area=width*height;
              return area;
}
};
int main()
{
 class rectangle r;
 r.setwidth(2);
 r.setheight(3);
 int area = r.getarea();
 cout<<"Area = "<< area <<endl<<"Paint Cost = "<<r.getcost(area);
 return 0 ;
}

Task3:
 Define a class display with three member functions named “print” first print function should print an integer passed to it, the second function should print a floating passed to it and third function should print a character array/string passed to it. Declare an object in main function of the class you defined and test the functions you made.

Program Code:

#include<iostream>      
#include<string>
#include<conio.h>
using namespace std;
class display
{
      int a;
      double b;
      string c;
public:
       void print(int e)
{
            a=e;
                                                cout <<"INTEGER NUMBER = "<<a << endl;
}
       void print(double f)
{
            b=f;
                                                cout <<"FLOAT NUMBER = "<<b << endl;
}
       void print (string d)
{
            c = d;
                                                cout <<"STRING : "<<c << endl;
}     
};
int main()
{
    display obj;
    obj.print(2);
                obj.print(22.5);
                obj.print("hello");
  return 0 ;
}

Task4:
Define a class named; complex with two data members real and imaginary and setter/getter member functions. Overload an addition “+” operator and multiplication “*” operator for your class. Declare two objects in main function of the class you defined and test the functions and operators you overloaded.

Program Code:               

#include<iostream>
#include<conio.h>
using namespace std;
class Complex
{
      float real;
      double imaginary;
public:  
       void setreal(float R )
{
       real=R;
}
       void setimaginary(double I)
{
       imaginary=I;
       float getreal()
{
       //real=1.33;
       return real;
}
       double getimaginary()
{
      // imaginary=-1;
       return imaginary;
}
       Complex operator+(const Complex& c)
{
       Complex complex;
       complex.real = this->real + c.real;
       complex.imaginary = this->imaginary + c.imaginary;
       return complex;
}
       Complex operator*(const Complex& co)
{
       Complex comp;
       comp.real = this->real*co.real;
       comp.imaginary = this->imaginary * co.imaginary;
       return comp;
}
};
int main()
{
    Complex comp;
    comp.setreal(6.34);
    comp.setimaginary(-9.3);
    int real = comp.getreal();
    cout<<" real number : "<< real <<endl;
    float imaginary=comp.getimaginary();
    cout<<" imaginary number : "<<  imaginary <<endl;  
    return 0 ;
}

Task5:
 Re-Write following example and test the results:

Program Code:                

#include<iostream>
#include<conio.h>
using namespace std;
class Shape
{
      protected:
      int width,height;
      public:
      Shape(int a=0 ,int b=0)
{
      width=a;
      height=b;
}
int area()
{
    cout<<"Parent class area :"<<width*height<<endl;
    return 0;
}
};
class Rectangle:public Shape
{
      public:
                                //  Rectangle(int a=0, int b=0):Shape(a,b)
 int rectangle;
          int rec(int width,int height)
{
//      int getarea()
//{
        rectangle=(width*height);
      cout<<"Rectangle class area :"<<rectangle<<endl; 
      return rectangle;
}
};
class Triangle:public Shape
{
public:
double triangle;
                double tri(int width,int height){
     triangle= (width*height/2);
                cout<<"Triangle class area :"<<triangle<<endl;
                return triangle;
    }
};
int main()
{
    Shape *shape;
    Rectangle b;
     b.rec(10,7);
    Triangle a;
    a.tri(10,5);
   shape=&b;
    //shape->area();
    //shape=&a;
    return 0 ;
}

Task6:
Write a c++ program and try to use all the concepts you learned in object oriented programming course.

Program Code:              

#include<iostream>
#include<conio.h>
using namespace std;
 class  Animal
{
       public:
             void move()
{
            cout<<"\nMOVE OF WILD ANIMAL";
}
};
 class horse:public Animal
{
       public:
             void move()
{
            cout<<"\n\t\n RUN";
}
};
int main()
{
 Animal *a;
 a=new Animal;
 a->move();
 delete a;
 horse *h;
 h=new horse;
 h->move();
 return 0 ;

}

          

No comments:

Post a Comment

Must drop your comments about post.