• Have Any Question?
  • contact@eitworld.com

cpp classes objects





C++ Classes and Objects

classes in c++

  • Class is a base of the object oriented programming language.
  • It is a user-defined data type that is a collection of variables of different-different data types and functions.
  • Class binds the data-members and member functions to work together.

C++ Class Definitions

    • A class in C++ is declared using the class keyword followed by the name of the class.
    • The class-name must follow the rules of writing identifier because class is also an identifier.
    • The class is opened by opening braces ‘{‘ and closed by closing braces ‘}’
    • The class definition must be end with semicolon.
    • Inside a class its data-members and member functions are defined.
    • Here is a basic syntax of the class-declaration:

class class_name

{

public:

data-members;

member-functions;

private:

data-members;

member-functions;

protected:

data-members

member functions;

};

      • Data members or member function may be defined as public, private or protected. These are the access-specifiers of the members of a class that defines the visibility of members of class in the program.

    1)public members of class can be used inside or outside the class.

 

    • 2)private members of class can only be used inside the class.

 

    • 3)protected members of class can used inside the containing class as well as its derived class.

 

  • The default access specifier of member of classes is private.

objects in c++

  • Object is instance of class which can access all the members of class.
  • A class is an abstract concept means it is not exist in really but object is a realistic concept which contains all properties of the class. For example if vegetable is a class then potato, leady-finger, bringle etc are its object.
  • The actual instance of class is created by making its objects.
  • All objects of the class have their own copies of variables and method defined in the class.

Define C++ Objects

    • The basic syntax to define C++ object is as follows:
class-name object-name;
    • After creating an object of class we can access the members of class through the object.The basic syntax for accessing the member of class through object is as follows:

object-name.variable-name;

object-name.function-name();

    • Here is an example-program that is sufficient to understand the concept of C++ classes and objects:

#include<iostream.h>
class program

{

private:

int a , int b;

public:

void read(int x, int y)

{

a=x;

b=y;

}

void show()

{

cout<<“a=”<<a<<“,b=”<<b;

}

};

void main()

{

program p1;

p1.read(4,5);

p1.show();

}

 

    • In the above example we use member-functions to access private data members of class because we cannot access private members directly with dot operator outside the class. public data members or member functions can be accessed outside the class with the help of object using dot operator as p1.read(4,5) and p1.show() in the above example.
    • The output of the above program is as follows:
a=4,b=5

Defining member function outside the class

    • We can also define the member function outside the class.
    • To define member function outside the class we have to use the scope resolution operator (::) to tell the compiler to which class the function belongs.
    • The basic syntax of it is given here:
Return-type class-name :: function-name (paramtere-list) { Function-body; }
    • Let us consider a simple example for defining a member function outside the class:

#include<iostream.h>
class program

{

private:

int a , int b;

public:

void read(int, int);

void show():

};

void program::read(int x, int y)

{

a=x;

b=y;

}

void program::show()

{

cout<<“a=”<<a<<“,b=”<<b;

}

void main()

{

program p1;

p1.read(4,5);

p1.show();

}

 

    • The output of the above program is as follows:
a=4,b=5

Constructor and Destructor

Class Constructor

      • Constructor is a special type of member function of class whose name is same as the name of the class in which is it is declared.
      • The main aim of the constructor is to initialize the data members of the class.
      • Constructor does not contain any return type.
      • Constructor is called when the object of the class is created in which it is declared.

Types of constructor:

        • In C# constructor can be of two types as follows:

      A) Non-parameterized constructor: It is also called default constructor. It does not contain any parameter. B) Parameterized constructor: Parameterized constructors are the constructors which contains parameters.

      • Here is an example which shows the concept of constructors:

#include<iostream.h>
class program

{

private:

int a,b;

public:

program()

{

a=0;

b=0;

cout<<“nThis is default constructor”;

}

program( int x, int y)

{

a=x;

b=y;

cout<<“nThis is parameterized constructor”;

}

void show()

{

cout<<“a=”<<a<<“,b=”<<b;

}

};

void main()

{

program p1;/*calling default constructor*/

cout<<“n Object1 data:”;

p1.show();

program p2(3,4);/*calling parameterized constructor*/

cout<<“nObject2 data:”;

p2.show();

}

      • The output of the above program is as follows:

This is default constructor

Object1 data:

a=0,b=0

This is parameterized constructor

Object2 data:

a=3,b=4

Destructor

      • A destructor is a member function of the class whose name is same as the class but preceded with a tilde sign (~).
      • The purpose of destructor is to destroy the object if it is no longer needed.
      • It has no return type.
      • It does not contain any parameter.
      • A class can only have one destructor.

Here is an example of destructor as follows:


#include<iostream.h>
class program

{

private:

int a,b;

public:

program()

{

a=0;

b=0;

cout<<“nThis is a constructor”;

}

~ program()

{

cout<<“nThis is a destructor”;

}

void show()

{

cout<<“a=”<<a<<“,b=”<<b;

}

};

void main()

{

program p1;/*calling constructor*/

cout<<“n Object1 data:”;

p1.show();

}

The output of the above program is as follows:

This is a constructor

Object1 data:

a=0, b=0

This is a destructor



Real Time Web Analytics