Skip to main content

what is class in c++

Hello friends, in this post today, we will learn about what is class in c++ and how it is accomplish in c++. so let's start....

What is class in c++, what is class
What is class



1. introduction to class:-

  •  A class is a template that specifies the attributes and behavior of things or object
  • Class means to achieve encapsulation.
  • A class  is a user defined new data type like Student, Book, Account detail etc.
  •  A class is a blueprint or prototype for the  objects which are created.
  •   A class is an implementation of an abstract data type (ADT).
  •  It defines attributes and methods.
  •   Class is a description of object’s property  set and set of operations.
  •   For creating a new class we use class keyword.
  •  The body of class must be enclosed within curly braces({}) and terminated by a semicolon(;).

 2. some real life examples:-

1. Animal can be stated as a class and Dog, Python, Deer, Monkey, Cow, etc., are its  object.

2. Person can be stated as class and Ram, Rahul, Teacher, Student etc., are its objects.

3. Musician can be stated as a class and Arman malik, Anu Malik, Sonu nigam are its  objects.

 

3. syntax of class:-

class class_name
{
    Access specifiers:
   data members;
   member functions;

   Access specifiers:
   data members;
   member functions;

      ................;
     .................;
};

4. Accessing class member:-


  • Private members of the class can only accessed by the members functions with in that class.
  •  The class members ( functions or data), which are declared as public can be accessed from outside the class 
  • The class members, which are declared as Protected  can be accessed within the class and from derived class but cannot be accessed from any other class.
  • public, private, and public these are access specifiers or visibility labels  that control the flow of data member and function outside the class.
  • Remember:-If we not declare any visibility mode in our program, all data members and function become private.
  • If you are accessing  class member outside class can be done by using dot operator (.) and object of that class using following syntax,
  • object-name.function-name(actual-arguments);


#include <iostream>
using namespace std;

class smallob //define a class
{
    private: //private is a access specifiers it may be public, protected
           int somedata; //class data

    public:
          void setdata(int d) //member function to set data
          { 
              somedata = d;
          }

          void showdata() //member function to display data
           { 
               cout <<"Data is "<< somedata << endl
           }
};

int main()
{
smallob s1s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display data
s2.showdata();
return 0;
}

Output:-

Data is 1066
Data is 1776

5. Explanation of above example:-

  • smallobj is the name of class.
  • somedata is a data  member of the class type smallobj.

  • setdata and showdata are the member functions of the class type smallobj.

  • s1 and s2 are the object of smallobj.



6. Defining member Functions:-

  • Member functions of a class can be defined two ways:-

           1.Inside the class definition

           2.Outside the class definition

  •   The function definition in both class would be identical except for the way in which      the header is defined.

6.1  Inside the class definition:-

  • Member function can be define inside the class as illustrated in example. where both the functions are defined inside the class.
  • A member function defined inside a class is treated as an inline function. There, only small function must be defined within the class definition.


#include<iostream>
using namespace std;

class complex //define a class
{
    private: //private is a access specifiers it may be public, protected
    int a,b; //class data

    public:
    void setdata(int x,int y) //member function
    {
        a=x;
        b=y;
    }
    void showdata() //member function
    {
        cout<<"a="<<a<<endl<<"b="<<b;
    }
};

int main()
{
    complex c1; //objects of class
    c1.setdata(5,7); //call member function
    c1.showdata(); //call member function
    return 0;
}

Output:-

a=5
b=7


6.2 Outside the class  definition:-

  • when prototype of member functions are declared inside a class, they must be defined separately outside the class with the help of scope resolution operator represented as double colon ( :: ).

#include<iostream>
using namespace std;
class complex
{
    private:
    int a,b;

    public:
    void setdata(intint);
    void showdata();
};

void complex::setdata(int x ,int y)
{
   a=x;
   b=y;
}

void complex::showdata()
{
        cout<<"a="<<a<<endl<<"b="<<b;
}

int main()
{
    complex c1; //objects of class
    c1.setdata(10,20); //call member function
    c1.showdata(); //call member function
    return 0;
}

Output:-

a=10
b=20


7. some special characteristics of member functions are:

  • The same function name can be used in several different classes, since the membership label will identify the class to which they belong.
  • Member function are allowed to access private data of that class.
A function member can call another function member directly, without using the dot operator.



I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.

Thank you so much for reading. And take care about yourself and your family.


Comments

Post a Comment

Popular posts from this blog

Constructor in c++

Hello friends, in this post today, we learn about  what is constructor in c++ and types of constructor . so lets start... Constructor in c++ Constructor:-   A constructor is a “special” member function which initializes the objects of class.  Constructor is used to solve problem of initialization. Properties of constructor:- Constructors are used to construct the object of the class. Constructor name must be same as class name. Constructor is invoked automatically when objects of the class are created. The constructors are always declared in the public section. If declared in the private section then objects are can only be created inside the member functions but serve no purpose. It must be an instance member function , that is, it can never be static. Constructors  do not have any return type not even void so they cannot return any value. Constructor can also be overloaded. Constructors cannot be inherited, but they can be called from the constructors of...

Function with Default arguments in c++

Hello friends, i n this post today, we learn about   Function with Default arguments in c++ .  so lets start... Function with default arguments  Function with default arguments:- In C ++ it is possible for a function not to specify all its arguments. Some of the arguments  may be specified their default values at the time of declaring the function.   Default values are specified when the function is declared. We must add default arguments from right to left. We cannot provide a default value to a particular argument in the middle of an argument list. Default arguments are useful in situations where some arguments always have the same value. For Example, passing marks. In a function with default argument, if one argument is default, all successive arguments  must be default. We cannot provide default values in the middle of the arguments or towards  l eft side. We provide few examples: 1. void fun(int x, int y = 20, int z=35); (valid) 2. void fun(int x,...

Pure virtual function in c++

Hello friends, i n this post today, we learn about what is pure virtual  function in c++ and how to use pure virtual function  in c++ .  so lets start... Pure virtual function in c++ Pure virtual function:- A pure virtual function is a function which has its body set to 0 i.e., the pure virtual function  does not have any body. A pure virtual function means ‘do nothing’ function. A function declared in a way: Syntax:- virtual void display() = 0;  is known as pure virtual function. Here = 0 does not mean that function show is equal  to 0. It simply means that the virtual function show has no body.  The pure virtual function act  as an interface and any class which inherits the class in which pure virtual function is present,  has to provide the implementation for the function show. We can say empty function. A pure virtual function has no definition relative to the base class. Programmers have to redefine pure virtual function in derived clas...