C++21: Classes and Objects

Object Oriented Programming with C++

Features:

1)Data Abstraction and Encapsulation:

     Abstraction: The feature indicates that here we can describe any imaginary and        non imaginary entity in programming.

   Encapsulation: Encapsulation means to bind some thing  in a group. The ability to     combine data and functions together is called as encapsulation.


2) Inheritance:

        Reusability of the existing classes without distrusting the existing classes is                called as inheritance. Inheritance supports to reuse existing code.

3) Polymorphism:

       Polymorphism indicates similar behavior of objects of different classes types. It        is a technique which is used to  develop a code which will work for different               objects of different classes.

 

Class: The class is the programmatical description of a entity or we can say that class is a blue print for creating objects.

Object: Every imaginary and non-imaginary thing(i.e. entity) to which we can describe programmatically is called as object.

Example:

#include <iostream>
using namespace std;
class Rectangle{

    private:
        int LB;

    public:
    void setdata(int p,int q)
    {
        L=p;
        B=q;
    }

        void area()
        {
            int A= L*B;
            cout<<"Area is : "<<A<<endl;

            
        }
        void perimenter()
        {
            int P= 2*(L+B);
            cout<<"Perimeter is : "<<P<<endl;


        }

};
int main()
{
    Rectangle x,y;

    x.setdata(5,4);
    x.area();
    x.perimenter();

    y.setdata(5,4);
    y.area();
    y.perimenter();


    return 0;
}

Output:

Area is : 20
Perimeter is : 18


WAP to create a class with member roll no, depat, name with member function setdata and showdata. 
#include <iostream>
using namespace std;
class Student
{
    private:
        int RN;
        char SN[20],D[20];

    public:
        void setdata(int rn,char n[], char d[])
        {
            RN = rn;
            strcpy(SN,n);
            strcpy(D,d);
        }

        void showdata(){
            cout<<"\n Roll number = "<<RN<<endl;
            cout<<"Student Name = "<<SN<<endl;
            cout<<"Department = "<<D<<endl;

        }
};
int main()
{
    //Design a class
    // *? Student with datamembere and name department define number functions setdat() and showdata()
    Student a,b;
    a.setdata(111,"Jack","CSE");
    a.showdata();

    b.setdata(122,"James","IT");
    b.showdata();



    return 0;
}

Output:

Roll number = 111
Student Name = Jack
Department = CSE

 Roll number = 122
Student Name = James
Department = IT

WAP to create class Employee with members name, age and salary and member functions set data and show data. Also create objects of it.

#include <iostream>
using namespace std;
class Employee{

    private:
        char EN[20];
        int ES;
        int EA;

    public:

    void setdata(char n[],int s, int a)
    {
        strcpy(EN,n);
        ES = s;
        EA = a;

    }

    void showdata(){
        cout<<"\n Name of Employee : "<<EN<<endl;
        cout<<"Salary of Employee : "<<ES<<endl;
        cout<<"Age of Employee : "<<EA<<endl;
    }
};



main()
{
    Employee a,b;

    a.setdata("Harry",20000,24 );
    a.showdata();
    
    b.setdata("Ayush",2000,17 );
    b.showdata();

}

Output:

 Name of Employee : Harry
Salary of Employee : 20000
Age of Employee : 24

 Name of Employee : Ayush
Salary of Employee : 2000
Age of Employee : 17

WAP to create a class distance with member foot and inch with setting and display function. Also create a func convert to foot into inches.

#include <iostream>
using namespace std;
class Distance{

    private :
    int F;
    int I;

    public:
    void setdata(int f,int i){
        F =f;
        I = i;

        

    }
    int convert(){
        return(F*12+I);
        

    }
    void showdata(){
        cout<<"Feet: "<<F<<" Inches: "<<I<<endl;
      
    }

};


main(){
    
    Distance a;

    a.setdata(5,2);
    a.showdata();

    int x = a.convert();

    cout<<"The value in Inches is: "<<x<<endl;


}

Output:

Feet: 5 Inches: 2
The value in Inches is: 62
Design a class currency with data member rupee and paise having display and setdata fun. Also define a function convert to convert paise into rupee.

#include <iostream>
using namespace std;
class Currency{

    private:
    int R,P;

    public:

    void setdata(int r,int p){
        R =r;
        P = p;
    }

    void showdata(){

        cout<<"Rupees: "<<R<<" Paise: "<<P<<endl;
    }

    int convert(){
        return (100*R + P);
    }
};


main(){

    Currency a; //Object

    a.setdata(1250);
    a.showdata();

    int r = a.convert();

    cout<<"Ruppes in paise is : "<<r<<endl;


}

Output:

Rupees: 12 Paise: 50
Ruppes in paise is : 1250


WAP a program to create a class rectangle with members length and breadth. Also add function to find area and perimeter to the class.

#include <iostream>
using namespace std;
class Rectangle
{

private:
    int L;
    int B;

public:
    void setdata(int l, int b)
    {
        L = l;
        B = b;
    }
    void area()
    {
        int A = L * B;

        cout << "Area of rectangle is : " << A << endl;
    }

    void perimeter()
    {
        int P = 2 * (L + B);

        cout << "The Perimeter of Rectangle is: " << P << endl;
    }

};

int main()
{

    Rectangle a;

    a.setdata(54);
    a.area();
    a.perimeter();

    Rectangle b;

    b.setdata(10,30);

    b.area();
    b.perimeter();
    

    return 0;
}

Output:

The Perimeter of Rectangle is: 18
Area of rectangle is : 300
The Perimeter of Rectangle is: 80








Comments