What is class in oops with example (Class in C++ and Java)

What is class in oops with example (Class in C++ and Java)

Class in an Object oriented computer programming language such as C++ and java is a template. It is a blueprint. Class is user defined data type.

Class is a user defined data type. Class works as a blueprint to declare an object. Class and objects are the canter point of object oriented programming. Object is an instance of class.

Class consists of data members and functions. Functions works on the data members declared in the class.

In object oriented UML notations class is represented in the following three sections:

  • Name of the class
  • Data members of the class
  • Functions of the class

 

What is class in object oriented programming with example

Class is a user defined data type. It is designed and defined by the user according to the convenience.

Example –

Name of the class: Person

Data members of class:

Person_id

Person_name

Person_address

Functions of the class:-

Inputdata()

Displaydata()

 

There three modifiers private, public and protected. By default all the members of a class are private.

Generally all the data members are declared as private and all the functions comes under the public section. Protected modifier is used to make a class inheritance ready.

Implementation of the above class is as below-

// Implementation of class using C++

Class person

{

Private:

Int person_id;

Char person_name[25];

Char person_address[50];

Public:

Void inputdata()

{

Cout <<“enter person id”;

Cin>>person_id;

Cout <<”enter name of the person”;

Cin>>name;

Cout<<”enter address”;

Cin>>address;

}

Void display()

Cout <<person_id;

Cout<<person_name;

Cout<<person_address;

}

};

What is a class and an object

Object is an instance of class. Object works like a variable. As integer or int is a data type. In int x, x is a variable which takes two bytes same object is data type which takes as many bytes as the data members are there in the class private section.

To declare an object of the above mentioned class you will write following line:

Person person1, person2;

Person1 and Person2 are objects of class person.

Why do we use class in OOPs

What is class in object oriented programming with example

Thanks for reading, Welcome to your comments on this Post

This site uses Akismet to reduce spam. Learn how your comment data is processed.