what is class in c++ ?

  • Classes are user-defined data-type, which contains collection of data members and member functions on which objects works.
  • Once a class has been declared, we can create any number of objects of that class.
  • Objects are variables of the type class.
  • A class is a collection of objects of similar types.
  • A class is a collection of objects of similar types.
  • A class is defined in C++ using keyword “class” followed by the name of class.
  • The body of class is defined inside the curly brackets and terminated by a
  • semicolon at the end.

syntax:-

class className
{
// some data
// some functions
};


Example: Class in C++
class Test
{
private:
int data1;
float data2;

public:
void function1()
{ data1 = 2; }

float function2()
{
data2 = 3.5;
return data2;
}
};


Here, we defined a class named “Test”.
This class has two data members: data1 and data2 and two member functions:
function1() and function2().
  •  Class members can be private, public, or protected.
  • If fruit has been defined as a class, then the statement
  • Fruit mango;
  • Will create an object “mango” of the class Fruit.



Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post