In this c++ tutorial, you will learn and understand the following with program examples. They are
- Class and objects
- Local object example
- Global object example
- Class with inside the member function
- Class with outside the member function.
CLASS
- Class is a set of objects that shares common structure (variables) and behaviors (functions)
- It is a user defined type
- It contains both variables and functions
- Class is created using the reserved word named class
- By default, all the members in the class are private modifier.
OBJECT
- It is an instance of a class
- It is used to access instance variables and methods
- It will be created automatically, when a constructor of the same class is created
- Memory will be created, when an object of a class is created
- Many objects are allowed per class.
CLASS DEFINITION
- The class definition contains two sections. They are:
- Variable section
- Function section
Syntax
Class Members
CHARACTERISTICS OF A CLASS
- It must be defined using the class keyword.
- It can contain variables and functions
- It can contain static variables & static methods
- It can be derived from another class (using colon : keyword)
- A class can be preceded with an access modifier.
- Private is a default modifier to variables and functions of class.
ADDING VARIABLES
- Any number of variables can be added to the class.
- These variables that are declared inside the class are called Instance / Static variables.
- The declaration of instance variables doesn’t occupy any space in memory. At the time of creation, they will occupy the memory to some extent.
ADDING FUNCTIONS
- Any number of functions can be added in a class.
- These functions that are defined inside a class are called instance methods.
Usage
- It is used to provide the implementation of variables(data).
OBJECT CREATION
- An object is an instance of a class.
- Unlike java / c#, the c++ objects are value type. So, no need to use new modifiers in the creation of objects.
- An object is a block of memory that contains the space (memory) to store all the instance variables
- Memory will be allocated when objects are created in the class
- It can be a local object or global object.
Two Ways of creating an object
- In c++, we can create an object using two ways. They are:
- Simple declaration
- Pointer
1. SIMPLE DECLARATION (NORMAL OBJECT)
- This is actually a variable (object) not pointer
- Static object
- It will be stored on stack memory. Because this is value type.
- It will be automatically destroyed when it goes out of the scope
- Default constructor will be called when an object is created.
2. POINTER (POINTER OBJECT)
- This is dynamic method of creating an object with help of new operator
- This is the dynamic object
- Here the object will not be destroyed when it goes out of the scope. The “delete <object-name>” operator is explicitly used to delete the objects
- This type of an object will be stored on heap memory
- It returns a pointer.
DIFFERENCE BETWEEN STATIC AND DYNAMIC OBJECT
S.N | NORMAL OBJECT | POINTER OBJECT |
1. | Static object | Dynamic object |
2. | This object is created without using new modifier | This object is created with help of new modifier |
3. | This object is value type. So, it will be stored on stack memory | This object is reference type based. So, it will be stored on heap memory |
4. | No need to use constructor name in the object creation Example: <class-name> <object-name>; | Constructor name is needed in the object creation Example: <class-name> <object-name>=new <constructor-name>; |
5. | Here object will be automatically destroyed when it goes out of the scope | Here object will not be automatically destroyed. The delete operator must be used to delete the object |
NOTE
- It is important to note that, when creating an object with a new modifier, we always have to delete it.
ACCESSING CLASS MEMBERS
- The instance data members (variables) and member functions (functions) of a class is accessed using object with dot (‘.’) operator
- The static data members (variables) and member functions (functions) of a class are accessed using class-name with dot (‘.’) operator.
I. EXAMPLE OF CLASS
(LOCAL OBJECT)
1. SOURCE CODE
#include<iostream>
using namespace std; // global namespace std.
class Welcome
{
public: // public method
void message()
{
cout<<“Welcome to c++ …\n”;
}
};
int main()
{
Welcome obj; // local object creation (with default constructor)
obj.message(); // calling instance method using object
return 0;
}
2. OUTPUT
II. EXAMPLE OF CLASS
(GLOBAL OBJECT)
1. SOURCE CODE
#include<iostream>
using namespace std; // global namespace
class Welcome
{
public:
void message()
{
cout<<“Welcome to c++ …\n”;
}
} obj;
int main()
{
obj.message(); // calling instance method using global object
return 0;
}
2. OUTPUT
DEFINITION OF MEMBER FUNCTION
- C++ provides two options for defining a function in a class. They are:
- Class with inside the member function
- Class with outside the member function
1. Class with inside the member function
- Here function definitions are placed in a class.
III. CLASS WITH INSIDE MEMBER FUNCTION
1. SOURCE CODE
#include<iostream>
using namespace std;
class Test
{
public:
int a,b,c;
void input()
{
cout<<“Enter the first number : “;
cin>>a;
cout<<“Enter the second number : “;
cin>>b;
c=a+b;
}
void output()
{
cout<<“Sum of two numbers are : “<<c<<“\n”;
}
};
int main()
{
cout<<“————————————\n”;
cout<<“\tInside the Function Definition\n”;
cout<<“————————————\n”;
Test obj;
obj.input();
obj.output();
return 0;
}
2. OUTPUT
2. Class with outside the member function
- It is an optional
- Here function definitions are placed outside of a class
- This is done by using the scope resolution operator (::)
IV. CLASS WITH OUTSIDE MEMBER FUNCTION
1. SOURCE CODE
#include<iostream>
using namespace std;
class Test
{
public:
int a,b,c;
void input();
void output();
};
void Test::input()
{
cout<<“Enter the first number : “;
cin>>a;
cout<<“Enter the second number : “;
cin>>b;
c=a+b;
}
void Test::output()
{
cout<<“Sum of two numbers are : “<<c<<“\n”;
}
int main()
{
cout<<“————————————-\n”;
cout<<“\tOutside the Function Definition\n”;
cout<<“————————————-\n”;
Test obj; // local object
obj.input(); // calling instance method via object
obj.output(); // calling instance method via object
return 0;
}
2. OUTPUT