22 Nov 2019

  • November 22, 2019
  • Amitraj
Class:-
-> Language supports different data type like int, float, char, long, structure etc. Similar to that C++ supports class data type.

-> A class is a user defined data type which has data members and member functions. They can be accessed through class variables (objects).

-> A class is like a Blue print for an object.

-> By default, All members in the class are Private.

For Ex:
Fruit is a class.
Then mango, orange will be objects of class.



Objects

-> Class type variable is known as object.
->  An object is an instance of a class.
-> Objects are basic runtime entities in an object oriented system.
-> In Structured programming problem is divided into function unlike this, in OOPs the problem is divided into objects.




// C++ program using class to calculate Area.

#include<iostream>
using namespace std;
class  shape
{
      private:
                      int l,b,area;
      public:
                     void input()
      {
              cout<<"Enter l and b values:";
               cin>>l>>b;
       }
void calculate()
{
         area = l*b;
}
void output()
{
        cout<<"Area is:"<<area;
}
};
int main()
{
      shape s;
      s.input();
      s.calculate();
      s.output();
      return 0;
}

OUTPUT:

Enter l and b values:6    5
Area is:30


*Abstraction using classes: An abstraction can be achieved using classes. A class is used to group all the data members and member functions into a single unit by using the access specifiers. A class has the responsibility to determine which data member is to be visible outside and which is not.




Translate

Popular Posts