ASSIGNMENT-1


1.create a class employee having data members empno,ename,desig,and sal.
write a member function(method) to initialize the data members and a display
method to show the information.create an object of class Employee in main function
and call the functions.

#include<iostream>
using namespace std;
#include<stdio.h>
class employee
{
    int empno;
    char ename[20];
    char desig[10];
    double sal;
public:
    void init();
    void disp();
};

void employee::init()
{
    cout<<"-------details of employee----------"<<endl;
    cout<<"enter the employee no:";
    cin>>empno;
    cout<<"enter the employee name:";
    fflush(stdin);
    gets(ename);
    cout<<"enter the designation of employee:";
    fflush(stdin);
    gets(desig);
    cout<<"enter the salary of employee:";
    cin>>sal;
}

void employee::disp()
{
    cout<<"employee no :"<<empno<<endl;
    cout<<"employee name :"<<ename<<endl;
    cout<<"designation :"<<desig<<endl;
    cout<<"salary :"<<sal<<endl;
}

int main()
{
    employee ob;
    ob.init();
    ob.disp();
    return 0;
}

2.Create a class product having data members prodid,prodname,price,unit and total.
define a member function to calculate the bill and display th bill amount.

#include<iostream>
#include<stdio.h>
using namespace std;
class product
{
    int prodid;
    char prodname[20];
    double price;
    int unit;
    double total;
   
public:
    void set();
    void disp();
};
void product::set()
{
    cout<<"enter the product id :";
    cin>>prodid;
    cout<<"enter the product name :";
    fflush(stdin);
    gets(prodname);
    cout<<"enter the price :";
    cin>>price;
   
}

void product::disp()
{
    cout<<"\nenter the details of the sell:"<<endl;
    int temp;
    cout<<"enter the product id;";
    cin>>temp;
    if(temp==prodid)
    {
        cout<<"how many units:";
        cin>>unit;
        total=unit*price;
        cout<<"product name :"<<prodname<<endl;
        cout<<"price :"<<price<<endl;
        cout<<"units :"<<unit<<endl;
        cout<<"price :"<<price<<endl;
        cout<<"totals :"<<total<<endl;
    }
    else
        cout<<"product is not available:"<<endl;
}

int main()
{
    product ob;
    ob.set();
    ob.disp();
    return 0;
}

3.Create a class shape having two data member length and width .
define some member methods to calculate the area of rectangle,square ,triangle and circle.

#include<iostream>
using namespace std;
class shape
{
    double length;
    double width;
    double area;
public:
    void rectangle();
    void square();
    void triangle();
    void circle();
};

void shape::rectangle()
{
    cout<<"enter the length of rectangle :";
    cin>>length;
    cout<<"enter the width of rectangle :";
    cin>>width;
    area=length*width;
    cout<<"area of rectangle :"<<area<<endl;           
}

void shape::square()
{
    cout<<"enter the length of square:";
    cin>>length;
    area=length*length;
    cout<<"area of square:"<<area<<endl;
}

void shape::triangle()
{
    cout<<"enter the base of rectangle:";
    cin>>length;
    cout<<"enter the height of rectangle:";
    cin>>width;
    area=0.5*length*width;
    cout<<"area of triangle :"<<area<<endl;
}

void shape::circle()
{
    cout<<"enter the radius of circle:";
    cin>>length;
    area=4*3.14*length*length;
    cout<<"area of circle:"<<area<<endl;
}

int main()
{
    char c;
    shape ob;
    do
    {
        cout<<"-----menu-------\n";
        cout<<"1.rectangle"<<endl;
        cout<<"2.square"<<endl;
        cout<<"3.triangle"<<endl;
        cout<<"4.circle"<<endl;
       
        cout<<"enter the choice:";
        int ch;
        cin>>ch;
        switch(ch)
        {
            case 1:ob.rectangle();break;
            case 2:ob.square();break;
            case 3:ob.triangle();break;
            case 4:ob.circle();break;
            default :cout<<"invalid choice:"<<endl;
        }
       
        cout<<"menu(press y or n)";
        cin>>c;
    }while(c=='y');
   
    return 0;
}

4.dont necesarry.and one more reason is that i m not unable to understand.Sorry

5.Declare a class money which contains two data members rs. and ps.
write a method to initialize its memeber where the input will be given
 by user(if the user has given paisa value more than 99 then it has to
be converted into rs. and paisa),and another method to display the value
 in terms of rupees and paisa.

#include<iostream>
using namespace std;
class money
{
    int rs,ps;
public:
    void init()
    {
        cout<<"enter the rs and ps :";
        cin>>rs>>ps;
        if(ps>99)
        {
            rs=rs+(ps/100);
            ps=ps%100;
        }
    }
   
    void disp()
    {
        cout<<"rupees :"<<rs<<endl;
        cout<<"paisa :"<<ps<<endl;
    }
};

int main()
{
    money ob;
    ob.init();
    ob.disp();
    return 0;
}
6. Declare a class time which has three data members hr,min,and sec.
Write a memeber method that stores the value in the respective data
members where the parameter for that method will be in terms of second only.

#include<iostream>
using namespace std;
class time
{
    int hr,min,sec;
public:
    void store();
    void display();
};

void time::store()
{
    cout<<"enter the time in second:";
    cin>>sec;
    if(sec>60)
    {
        min=sec/60;
        sec=sec%60;
        if(min>60)
        {
            hr=min/60;
            min=min%60;
        }
        else
        {
            hr=0;
        }
    }
    else
    {
        hr=0;
        min=0;
    }
}

void time::display()
{
    cout<<"hour :"<<hr<<endl;
    cout<<"minute :"<<min<<endl;
    cout<<"second :"<<sec<<endl;
}

int main()
{
    time ob;
    ob.store();
    ob.display();
    return 0;
}

7.Create a class account.an account has a balance,functions to add and withdraw money,
and a function to query the current balance ,charge a rs. 50 penality if an attempt is made to withdraw
more money than available in account.

#include<iostream>
#include<stdlib.h>
using namespace std;
class account
{
    double balance;
public:
    void add();
    void withdraw();
    void current();
};
int c=0;
void account::add()
{
    if(c==0)
    {
        cout<<"enter the balance to add:";
    cin>>balance;
    c++;   
    }
    else
    {
        cout<<"enter the balance to add:";
        double temp;
        cin>>temp;
        balance=balance+temp;
    }
}

void account::withdraw()
{
    cout<<"enter the balance to withdraw:";
    double temp;
    cin>>temp;
    if(temp>balance)
    {
        balance=balance-(temp+50);
        cout<<"u have been charged rs 50 to withdraw more money than available in account"<<endl;
    }
    else
    {
        balance=balance-temp;
    }
}
void account::current()
{
    cout<<"ur current balance is:"<<balance<<endl;
}

int main()
{
    account ob;
    char c;
    do
    {
        cout<<"1.add money"<<endl;
        cout<<"2.withdraw money"<<endl;
        cout<<"3.current"<<endl;
        cout<<"4.exit"<<endl;
        cout<<"enter the choice:";
        int ch;
        cin>>ch;
        if(ch==1)
            ob.add();
        else if(ch==2)
            ob.withdraw();
        else if(ch==3)
            ob.current();
        else if(ch==4)
            exit(0);
        else   
            cout<<"invalid choice:"<<endl;
        cout<<"menu(y/n)";
        cin>>c;
    }while(c=='y');
   
    return 0;
}


8.create a class mark that contains data member regnum,semnum,sub1,sub2,sub3,and avg.
Declare a method insert to input the data and calculate the avg mark.Declare an array of object
of size 5 for the class mark and initialise them.then display the values to user.

#include<iostream>
#include<stdio.h>
using namespace std;
class mark
{
    char name[20];
    int regnum;
    int semnum;
    float sub1,sub2,sub3,avg;
public:
    void insert();
    void display();
};
void mark::insert()
{
    cout<<"enter the student's name:";
    fflush(stdin);
    gets(name);
    cout<<"enter the regestration number:";
    cin>>regnum;
    cout<<"enter the semester number:";
    cin>>semnum;
    cout<<"enter marks of sub1:";
    cin>>sub1;
    cout<<"enter the marks of sub2:";
    cin>>sub2;
    cout<<"enter the marks of sub3:";
    cin>>sub3;
    avg=(sub1+sub2+sub3)/3;
}

void mark::display()
{
    cout<<name<<"\t\t\t";
    cout<<" "<<regnum<<"\t\t";
    cout<<"  "<<semnum<<"\t";
    cout<<"  "<<sub1<<"\t";
    cout<<"  "<<sub2<<"\t";
    cout<<"  "<<sub3<<"\t";
    cout<<avg<<"\n";
}
int main()
{
    mark ob[5];
    int i;
    for(i=0;i<5;i++)
    {
        cout<<"\nenter the details of"<<i+1<<" student"<<endl;
        ob[i].insert();
    }
    cout<<"name"<<"\t\t";
    cout<<"regestration number"<<"\t";
    cout<<"sem no"<<"\t";
    cout<<"sub 1"<<"\t";
    cout<<"sub 2"<<"\t";
    cout<<"sub 3"<<"\t";
    cout<<"average"<<"\n";
    for(i=0;i<5;i++)
    {
        ob[i].display();
    }
    return 0;
}


9.Create a class fraction that has two data members num and den.declare a method to set the
values and another method to display their reduce form(i.e 4/2 has to be displayed as 2/1).
create an object in main function and call the both methods.

#include<iostream>
using namespace std;
class fraction
{
    int num;
    int den;
public:
    void set();
    void display();
};

void fraction::set()
{
    cout<<"enter the numerator and denominator:";
    cin>>num>>den;
}

void fraction::display()
{
    for(int i=2;i<50;i++)
    {
        if(num%i==0 && den%i==0)
        {
            num=num/i;
            den=den/i;
        }
    }
   
    cout<<"number in reduced form :"<<num<<"/"<<den<<endl;
}

int main()
{
    fraction ob;
    ob.set();
    ob.display();
    return 0;
}

10.Create a class complex which has the data members real and imaginary.
and a se method to set the values and another method display to show the values which will
display the values in the format of(a+ib).Create an object of class complex and display their
values.

#include<iostream>
using namespace std;
class complex
{
    int real,imag;
public:
    void set();
    void display();
};
void complex::set()
{
    cout<<"enter the real and imaginary parts of complex no :";
    cin>>real>>imag;
}

void complex::display()
{
    if(imag>0)
    {
        cout<<real<<"+"<<imag<<"i"<<endl;
    }
    else
    {
        cout<<real<<imag<<"i"<<endl;
    }
}

int main()
{
    complex ob;
    ob.set();
    ob.display();
    return 0;
}

Total Pageviews