Object And Class | Details And Some Examples - LBM4

Recent Post

Monday 23 September 2019

Object And Class | Details And Some Examples

Objects

In OOPS,  Objects are variables of classes.

Object consists of the data and the functions that operate on the data. Object data is usually private: no functions other than those within the same object can access it. On the other hand, the functions within the object are usually public. They can be accessed by the other functions in the program including the functions in the other objects. So the function in one object can access the data in the other object through the function of that object.
C++

Class

It is a specification for any number of objects based on that class. Class itself is a template (data type) for objects. When we actually define the object, of a class we are requesting to allocate memory for that object to hold its data.

Data Member

The data items within a class are called data members. There can be any number of data members in a class. Normally, data members follow the keyword private, so they can be accessed from within the class but not from outside. That is why we say that oops have feature of data hiding. So, it is safe from accidental alteration.

class demo
{
private:
int rollno; //member data
float score; //member data
public:
setdata( int rl, float sc) //member function
{rollno=rl; score=sc;}
setdata( )
{
cout<<"Enter the roll no: \n";
cin>>rollno;
cout<<"Score: \n";
cin>>score;
}
showdata( ) //member function
{
cout<<"\Roll No: " <<rollno<<"has scored "<<score<<endl;
}
};
above class contains two data items: rollno and score. Here rollno is of type int and score is of type float.

Member Function

Member functions are functions that are included within a class. In above example there are two member functions in class demo: setdata( ) and showdata( ). Each function can have one or more statements. The functions setdata( ) and showdata( ) follow the keyword public which means that they can be accessed from outside the class. It is also possible to declare a function within a class and define it elsewhere.

The class demo can be used as follows

int main()
{
demo d1,d3;
d1.setdata(12,10.4);
d3.setdata( );
d1.showdata( );
d3.showdata( );
return 0;
}

Call
In oops. Objects are variables of classes. It is a specification for any number of objects based on that class. Class itself is a template (data type) for objects. When we actually define the object, of a class we are requesting to allocate memory for that object to hold its data.

Some Examples

Write a simple program that convert the temperature in degree Celsius to degree Fahrenheit and vice versa using the basic concept of class and object. Make separate class for Centigrade and Fahrenheit which will have the private member to hold the temperature value and make conversion functions in each class for conversion from one to other. For example you will have function toFahrenheit() in class Celsius that converts to Fahrenheit scale and returns the value.

 #include<iostream>
using namespace std;
class tocel
{
    float f;
    public:
    float conver(float)
    {
        cout<<"enter temperature in fahern.:"<<endl;
        cin>>f;
        returtn (5/9*(f-32));
    }


};
class tof
{
    float c;
public:
    float conver(float)
    {
        cout<<"enetr temperature in fahern:";
        cin>>c;
        return ((9/5)*c-32);
    }
};
main()
{
    tocel a;
    tof b;
    cout<<a.conver();
    cout<<b.conver();
    return 0;
}
Assume that you want to check whether the number is prime or not. Write a program that asks for a number repeatedly. When it finishes the calculation the program asks if the user wants to do another calculation. The response can be 'y' or 'n'. Don't forget to use the object class concept.
#include<iostream>
using namespace std;
class number
{
private:
    int num;
public:
    void getnum()
    {
    cout<<"enter a positive number:";
    cin>>num;
    }
    bool isPrime()
    {
        for(int i=2; i<(num/2); i++)
        {
            if(num%i==0)
                return false;

        else
        return true;
    }}
};
main()
{
    number n;
    char ch='y';
    while(ch=='y'){
        n.getnum;

    if(n isPrime()==true)
    {
        cout<<"prime"<<endl;
    }
    else
    {
        cout<<"not prime"<<endl;

    cout<<"want to continue?(y/n)"<<endl;
    cin>>ch;
    }}
    return 0;
}
Create a class called carpark that has int data member for car id, int data member for charge/hour and float data member for time. Set the data and show the charges and parked hours of corresponding car id. Make two member functions for setting and showing the data. Member function should be called from other functions.

#include<iostream>
using namespace std;
class carpart
{
    int carid,chargeperhour;
    float time, charge;
public:
    void getdata()
    {
        cout<<"enter car id:"<<endl;
        cin>>carid;
        cout<<"enter charge per hour:"<<endl;
        cin>>chargeperhour;
        cout<<"enter time:"<<endl;
        cin>>time;


    }
    void operate()
    {
        charge=chargeperhour*time;

    }
    void display()
    {
     cout<<"carid:"<<carid<<endl;
     cout<<"chargeperhour:"<<chargeperhour<<endl;
     cout<<"time"<<time<<endl;
     cout<<"total charge="<<operate()<<endl;
    }
};
main()
{
    carpart a;
    a.getdata;
    a.operate;
    a.display;
    return 0;

}
Write a program with classes to represent circle, rectangle and triangle. Each classes should have data members to represent the actual objects and member functions to read and display objects, find perimeter and area of the objects and other useful functions. Use the classes to create objects in your program.

#include<iostream>
using namespace std;
class circle
{
    float r;
public:
    float area(float r)
    {
        return 3.14*r*r;
    }
    float cercum(float r)
    {
        return 2*3.14*r;

    }
};
class rect

{
    float l,b;
public:
    float area(float l,float b)
    {
        return l*b;

    }
    float perim(float l,float b)

    {
    return 2*(l+b);
    }
};
class trig
{
    float s,a,b,c;
public:
    float area(float a,float b,float c)
    {
        float s;
        s=(a+b+c)/2;
        return s*(s-a)*(s-b)*(s-c);
    }
    float perim(float a,float b, float c)
    {
    return (a+b+c);
    }

};
main()
{
    circle x;
    rect y;
    trig z;
    cout<<"area and perimeter of circle="<<x.area(2.5)<<x.cercum(2.5)<<endl;
     cout<<"area and perimeter of rectangle="<<y.area(2,3)<<y.perim(2,3)<<endl;

    cout<<"area and perimeter of triangle="<<z.area(2,3,4)<<z.perim(2,3,4)<<endl;
    return 0;
}
Feel Free To Comment Some Question On this Topics

No comments:

Post a Comment