C++ language constructs with Objects and Classes
Namespace
Collection of related names or identifiers (functions, class, variables). Helps to separate identifiers from similar identifiers in other namespaces or global namespaces. It beings with keyword namespace. std
is a namespace whose members are used in the program. Its members are cout
, cin
, endl
, etc.
Overloading
Creation of two or more members with the same name but different in parameters.
Function Overloading
Having two function with the same name but different in parameters. Increases readability of the program because no need to use different names for the same action.
int add(int x, int y) {
return x+y;
}
int add(int x, int y, int z){
return x+y+z;
}
Operator Overloading
Operator is overloaded to provide special meaning to the user defined data type. It can be used to perform different operation on the same operand.
return_type class_name :: operator op(argument_list) {
//funtion body
}
scope operator(::)
, sizeof
, member selector(.)
, member pointer selector(*)
and ternary operator(?)
cannot be overloaded.
Inline functions
When we make an inline function, compiler places a copy of the code of that function at each point, where the function is called at compile time.
It saves memory space. If changes were made to the inline function, the entire function needs to be recompiled again.
inline return_type function_name(parameters) {
//function code
}
Default Argument
Used when we provide no argument or few argument while calling a function.
void temp(int = 13, float = 6.9);
int main() {
...
temp();
...
}
void temp(int i, float f) { //takes the default value declared above 13 & 6.9
//code
}
Introduction to Class and object
Class
It is a user defined data type that has members and functions.
class Student {
public:
int id;
string name;
string address;
}
No memory is allocated when class is defined, but is allocated when the object is created.
Object
It is an entity that has state and behavior. State means data and behavior means functionality.
Stduent s1; //creating an object of Student
Access Specifiers
It defines the scope (who can access) of the attributes and methods of a class.
Private
members cannot be accessed outside the class.
Public
member are accessible outside the class.
Protected
members can be accessed outside the class however, they can only be accessed by the inherited class.
In class
the default access is private but in structure
and union
the access is public.
Member Functions
It can accessed within the class definition or separately using scope resolution operator.
Types:
- Simple Function
- Static Function
- Const Function
- Inline Function
- Friend Function
Constructors
Constructor is a special type of function whose name should be same as the class name. It is invoked when the object is created and doesn’t have any return type.
Types
- Default Constructor: doesn’t take any parameters, used to initialize data members with real values.
- Parameterized Constructor: contains parameters (or arguments) in the constructor definition and declaration.
- Copy Constructor: member function that initialize an object using another object in the same class.
Constructor Overloading
Constructors with the same name but different parameters (or arguments).
Destructor
It destroys the class objects created by constructors preceded by the ~
symbol.
class Customer
{
public:
Customer();
~Customer();
};
It is not possible to define more than one destructor and is automatically called when the object goes out of scope.
Dynamic memory allocation for objects and object array
Memory allocation can be done in two ways: Static Allocation or Compile-time allocation and Dynamic Allocation or Run-time allocation.
Memory allocation is divided into two parts:
- Stack: All memory declared inside a function will take up memory from it.
- Heap: Used to allocate memory dynamically when program runs.
New Operator
It denotes the request for memory allocation on the free store.
int *p = new int;
Delete Operator
Releases the allocated memory.
C++ has new
and delete
for dynamically allocating memory whereas C has functions like malloc()
, calloc()
, realloc
, free
, etc.
For Arrays
char* pvalue = NULL; //request memory for the variable
pvalue = new char[20];
this
Pointer
It refers to the current instance of the class. It is used to pass current objects as parameters to another object.
Applications
- Return Object
- Method Chaining
- Distinguished Data Members
Static Data Member and Static Function
Static Data Member
Declared using the static
keywords, only visible within the class. Only one copy of static data member in the class, even if there are many class objects.
static data_type data_member_name;
Static Function
Declared using the static
keyword. Can be used to access static data members and can be called without creating an object of it.
Constant Member Functions and Constant Objects
Declared using the constant
keyword. Constant member function can only read or retrieve the data members of the calling class without modifying them.
Specifies the function is a read only function and doesn’t modify the object for the given class.
Friend Function and Friend Classes
Friend Function
Function declared using the friend
keyword that can access private, protected and public members of a class.
Friend Class
Declared using friend
keyword to have access to private, protected and public members of other class. Allowing to extend storage and access its parts while maintaining encapsulation.