Pages

Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Create a class of student, get student name and age and print on screen using setter and getter function.

Tuesday, 25 June 2013

#include <iostream>
#include <string.h>
using namespace::std;
class student
{
      string name;
      int age;
      public:
      student()     //constructor
      {
              
      }
      ~student()   //distructor
      {
               
      }
      void setData(string n, int a)     //setter function
      {
           name=n;
           age=a;
      }
      void Dispaly()                   //getter function
      {
           cout<<"Your name is "<<name<<"\nYour age is "<<age<<endl;
      }
};
int main()
{
       student a; // object of class student
       string name;
       int age;
       cout<<"Enter your name: ";
       getline(cin, name); //get complete line from user.
       cout<<"Enter your age: ";
       cin>>age;
       a.setData(name, age); // calling setter function
       a.Dispaly(); // calling getter function
       system("pause"); // hold screen untill user enter any character from screen
}

Write a program to create a class name employee and create three attributes and create a setter and a getter function for each attribute and call a default constructor in C++

Monday, 13 May 2013

#include <iostream>
#include <string>
using namespace::std;
class employee
{
      string first_name;
      string last_name;
      int salary;
      public:
             employee()
             {
                       first_name='NULL';
                       last_name='NULL';
                       salary=0;
             }
             void setFirstName()
             {
                  cout<<"Enter Your First Name: ";
                  cin>>first_name;
             }
             void setlastName()
             {
                  cout<<"Enter Your Last Name: ";
                  cin>>last_name;
             }
             void setSalary()
             {
                  cout<<"Enter Your Salary: ";
                  cin>>salary;
                  if(salary<0)
                  {
                              salary=0;
                  }
             }
             void dsplayFirstName()
             {
                  cout<<"Your First Name is: "<<first_name<<endl;
             }
             void displayLastName()
             {
                  cout<<"Your Last Name is: "<<last_name<<endl;
             }
             void displaySalary()
             {
                  cout<<"Your Salary is: "<<salary<<endl;
             }
};
int main()
{
    employee a1;
    a1.setFirstName();
    a1.setlastName();
    a1.setSalary();
    a1.dsplayFirstName();
    a1.displayLastName();
    a1.displaySalary();
    system("pause");
}

Scope resolution operator in C++

#include <iostream>
using namespace::std;
class date
{
      int day;
      int month;
      int year;
      public:
             int setdate(int, int, int);
             void display()
             {
                  cout<<day<<"/"<<month<<"/"<<year<<endl;
             }
};
int date::setdate(int d, int m, int y)
{
    day=d;
    month=m;
    year=y;
}
int main()
{
    int d, m, y;
    date a1;
    cout<<"Enter Day: "; cin>>d;
    cout<<"Enter Month: "; cin>>m;
    cout<<"Enter Year: "; cin>>y;
    a1.setdate(d, m, y);
    a1.display();
    system("pause");
}

Write a program to add and subtract complex equations.

Wednesday, 8 May 2013

#include <iostream.h>
class complex //create class of name complex
{
      double real1, imaginary1, real2, imaginary2, sreal, simaginary; //declare attributes
      public:
      complex() //constructor
      {
               real1=0;
               imaginary1=0;
               real2=0;
               imaginary2=0;
               sreal=0;
               simaginary=0;
      }
      //create functions
      void setvalues() //function to set values of equations
      {
           cout<<"Enter real number of first eqaution: ";
           cin>>real1;
           cout<<"Enter imaginary number of first eqaution: ";
           cin>>imaginary1;
           cout<<"Enter real number of 2nd eqaution: ";
           cin>>real2;
           cout<<"Enter imaginary number of 2nd eqaution: ";
           cin>>imaginary2;
      }
      void add()//function to add two complex equations
      {
           sreal=real1+real2;
           simaginary=imaginary1+imaginary2;
      }
      void sub()//function to subtract two complex equations
      {
           sreal=real1-real2;
           simaginary=imaginary1-imaginary2;
      }
      void answer() //dispaly answer on the screen
      {
           cout<<"{("<<sreal<<")"<<"+"<<"("<<simaginary<<")i}"<<endl;
      }
};
main()
{
      again: // lable for calculation again
      complex a; //create objects
      int b;
      char ask;
      a.setvalues(); //call function to set values of funtions.
      cout<<"Press 1 to add\nPress 2 to subtract\n";
      cin>>b;
      //ask from user wheather equations add or subtract.
      if(b==1)
      {
              a.add();
      }
      else if(b==2)
      {
           a.sub();
      }
      else
      {
          cout<<"Enter valid number.";
          goto again;
      }
      a.answer(); //call function to display or subtract
      //ask from user that he want to calculate again?
      cout<<"You want to claculate again? Y/N ";
      cin>>ask;
      if(ask=='Y' || ask=='y')
      {
           goto again;
      }
}

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 rupees toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected. Model this tollbooth with a class called toll Booth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes both of these to 0. A member function called payingCar()increments the car total and adds 50 to the cash total. Another function, called nopayCar(), increments the car total but adds nothing to the cash total. Finally, a member function called display() displays the two totals. [Updated]

Friday, 3 May 2013

#include <iostream.h>
class Tollboth   //class declaretion
{
      //attributes
      int cars;
      double collect;
      public:
             //contstructor
      Tollboth()
      {
           cars=0;
           collect=0;
      }
      //methods
      void payingcar() //paying car method
      {
           cars++;
           collect+=50;
           cout<<"Entry Succesfully added."<<endl;
      }
    
      void nopayingcar() //nonpaying  car method
      {
           cars++;
           cout<<"Entry Succesfully added."<<endl;
      }
    
      void display() //methods to display
      {
           cout<<"Total numbers of cars: "<<cars<<endl;
           cout<<"Total payment: "<<collect<<endl;
      }
};
main()
{
      int ask;
      char ag;
      Tollboth a; // create objects
      again:   //lable
      cout<<"Press 1 if payment is delievered by car owner.\n";
      cout<<"Press 2 if payment is not delievered by car owner.\nPress 3 to display total payment and cars.\nPress 1 or 2 or 3: "<<endl;
      //ask paying cara or nonpaying car.
      cin>>ask;
      if(ask==1)
      {
            a.payingcar();
      }
      else if(ask==2)
      {
            a.nopayingcar();
      }
      else if(ask==3)
      {
           a.display();
      }
      else
      {
           cout<<"Enter Valid Data."<<endl;
           goto again;
      }
      //ask to calculation again.
      cout<<"You like to enter data again? Y/N ";
      cin>>ag;
      if(ag=='Y' || ag=='y')
      {
           goto again; //goto lable again at above.
      }
}

Create a class Rectangle with attributes length and width, each of which Defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set functions should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0. [Updated]

#include <iostream.h>
class area //declare class
{
      float width, lenght, areaofrect; //attributes
      public:
      area() //construcor
      {
          width=1;
          lenght=1;
          areaofrect=0;
      }
      //functions
      void setwidth() //function to set width
      {
           again:
           cout<<"Enter width of rectangle: ";
           cin>>width;
           if(width<0 || width>20)
           {
                  cout<<"You cannot enter width greater than 20 and less than 0.\nEnter Valid Input."<<endl;
                  goto again;
           }
      }
      void setlenght() //function to set lenght
      {
           again:
           cout<<"Enter lenght of rectangle: ";
           cin>>lenght;
           if(lenght<0 || lenght>20)
           {
                  cout<<"You cannot enter lenght greater than 20 and less than 0.\nEnter Valid Input."<<endl;
                  goto again;
           }
      }
      void calculation() //function to calculate area of rectangle
      {
           areaofrect=width*lenght; //farmula of area of rectangle
      }
      void display() //function to display result
      {
           cout<<"Area of rectangle according to given parameters is: "<<areaofrect<<endl;
      }
    
};
main()
{
      again:  //lable
      area c; //create object
      char ask;
      //cla function of class area
      c.setwidth();
      c.setlenght();
      c.calculation();
      c.display();
      // ask from user to calculate again.
      cout<<"You want to calculate again? Y/N ";
      cin>>ask;
      if(ask=='Y' || ask== 'y')
      {
            goto again;
      }
}

Write a program in C++ to get employee name and its salary and print employee name, salary and grand salary(total salary+10% of salary) [Updated]

Monday, 22 April 2013


#include <iostream.h>
class employee
{
     char firstName[50];
     char lastName[50];
     int salary;
     public:
            employee()
            {
                strcpy(firstName, "NULL");
                strcpy(lastName, "NULL");
                salary=0;    
            }
            void setfn()
            {
                 cout<<"Enter First Name of Emplyee: ";
                 cin>>firstName;
            }
            void setln()
            {
                 char ln;
                 cout<<"Enter Last Name of Emplyee: ";
                 cin>>lastName;
            }
            void sets()
            {
                 int s;
                 cout<<"Enter salary of "<<firstName<<" "<<lastName<<": ";
                 cin>>s;
                 if(s<0){s=0;}
                 salary=s;
            }
            void dfn()
            {
                 cout<<"First Name is: "<<firstName<<endl;
            }
            void dln()
            {
                 cout<<"Last Name is: "<<lastName<<endl;
            }
            void dsalary()
            {
                 cout<<"Salary of "<<firstName<<" "<<lastName<<" of one year is: "<<salary*12<<endl;
                 cout<<"Salary with 10% of "<<firstName<<" "<<lastName<<" is: "<<((salary*10)/100)+salary<<endl;
            }
};
int main()
{
    char a;
    employee e1;
    again:
    e1.setfn();
    e1.setln();
    e1.sets();
    e1.dfn();
    e1.dln();
    e1.dsalary();
    cout<<"Would you like to put data of new employee: ";
    cin>>a;
    if(a=='y' || a=='Y'){goto again;}
}

Write a program to in C++ to get student name and age and print it on screen using OOP technique.


#include<iostream>
using namespace std;
class student{
char name;
int age;
public:
void setname(char newname,int newage)
{
name=newname;
age=newage;
}
void display()
{
    cout<<"your name is "<<name<<"\nYour age is "<<age<<endl;
}
};
int main()
{
student data;
data.setname('ammar',12);
data.display();
system("pause");
}

Write a program to print bill in C++ using oop


#include <iostream.h>
class bill
{
    char name[50];
    int unit, p_unit, c_unit;
    float price_unit, tbill, tax;
    public:
           bill()
           {
                price_unit=3.80;
           }
    void setname()
    {
       cout<<"Enter customar name: ";
       gets(name);
    }
    void setunits()
    {
        again:
        cout<<"Enter previous units: ";
        cin>>p_unit;
        cout<<"Enter current units: ";
        cin>>c_unit;
        if(c_unit<p_unit)
        {
             cout<<"Enter Valid data(Units)."<<endl;
             goto again;          
        }
        unit=c_unit-p_unit;
    }
    void make_bill()
    {
         float temp;
        if(unit<=100)
        {
                  temp=price_unit*unit;
                  tax=(temp*8)/100;
                  tbill=temp+35+tax;
        }
        else if(unit>100 && unit<=200)
        {
             temp=unit*(price_unit+3);
             tax=(temp*12)/100;
             tbill=temp+35+tax;
        }
        else
        {
            temp=unit*(price_unit+6);
             tax=(temp*14)/100;
             tbill=temp+35+tax;
        }
    }
    void display_bill()
    {
         int counter=0, i;
         for(i=1; i<=4; i++)
         {
                  cout<<"***********************************"<<endl;
                  counter++;
                  if(counter==2)
                  {
                                cout<<"Name: "<<name<<endl;
                                cout<<"Your total units are: "<<unit<<endl;
                                cout<<"You total bill is: R.s "<<tbill<<endl;
                  }
         }
    }
    void detail()
    {
         if(unit<=100){price_unit=3.80;}
         else if(unit>100 && unit<=200){price_unit=6.80;}
         else{price_unit=9.80;}
         cout<<"Total Units consumed: "<<unit<<endl;
         cout<<"PTV fee: R.s 35"<<endl;
         cout<<"Price Per unit at "<<unit<<" units is"<<price_unit<<endl;
         cout<<"Tax: "<<tax<<endl;
       
    }
};
main()
{
      char a, d;
      bill c;
      next:
      c.setname();
      c.setunits();
      c.make_bill();
      c.display_bill();
   
      cout<<"Press \"D\" or\"d\" to get detail of you bill"<<endl;
         cin>>d;
         if(d=='D' ||d=='d')
         {
                   c.detail();
         }
      cout<<"You want to calculate bill of next customar? Y/N ";
      cin>>a;
      if(a=='y' ||a=='Y')
      {
                cout<<"***********************************"<<endl;
                goto next;
      }
}

 

Search Box

Most Reading

Contact Form

Name

Email *

Message *