C++ Nelson Notes Unit 3
C++ Nelson Notes Unit 3
Define Class:
Class is collection of similar types of objects. Also, Class is a user defined data type.
Class is used to binds data and member functions.
When a class is defined, no memory is allocated but when it is instantiated (i.e. an object
is created) memory is allocated.
The general syntax or general form of the class declaration is as follows:
Defining Class
The class specification can be done in two part:
(i) Class definition. It describes both data members and member functions.
(ii) Class method definitions. It describes how certain class member functions are coded.
In C++, the member functions can be coded in two ways :
(a) Inside class definition
(b) Outside class definition using scope resolution operator (::)
Inside Class Definition:
When a member function is defined inside a class, we do not require placing a membership label
along with the function name. We use only small functions inside the class definition and such
functions are known as inline functions.
Outside Class Definition Using Scope Resolution Operator (::)
In this case the function’s full name (qualified_name) is written as shown:
Name_of_the_class :: function_name
The syntax for a member function definition outside the class definition is :
return_type name_of_the_class::function_name (argument list)
{
body of function
}
Here the operator::known as scope resolution operator helps in defining the member function
outside the class.
DECLARATION OF OBJECTS:
The objects of a class are declared after the class definition.
Syntax
Class_name object;
Example
test obj1;
Example
class test
{
public:
string geekname;
int id;
// printname is not defined inside class defination
void printname();
// printid is defined inside class defination
void printid()
{
cout << "Geek id is: " << id;
}
};
// Definition of printname using scope resolution operator::
void test::printname()
{
cout << "Geekname is: " << geekname;
}
int main() {
test obj1;
obj1.geekname = "xyz";
obj1.id=15;
obj1.printname();
cout << endl;
obj1.printid();
return 0;
}
1) Default Constructor
A default constructor doesn’t have any parameters (or arguments )
Example Program for Default Constructor
#include <iostream.h>
#include <conio.h>
class student_info
{
public:
//Default constructor
student_info()
{
cout<<" Welcome to Student Info Constructor Program "<<endl;
}
};
void main()
{
student_info obj1;
}
class Add
{
public:
//Parameterized constructor
Add(int num1, int num2)
{
cout<<(num1+num2)<<endl;
}
};
void main()
{
Add obj(10, 20);
}
Output is : 30
Copy constructor
Copy constructor is used to declare and initialize an object from another object.
class A
{
public:
int x;
A(int a) // parameterized constructor.
{
x=a;
}
A(A &i) // copy constructor
{
x = i.x;
}
};
int main()
{
A a1(20); // Calling the parameterized constructor.
A a2(a1); // Calling the copy constructor.
cout<<a2.x;
return 0;
}
~class_name()
{
//Some code
}
Similar to constructor, the destructor name should exactly match with the class name. A
destructor declaration should always begin with the tilde(~) symbol as shown in the syntax
above.
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
void main()
{
Employee e1;
Define Overloading?
C++ allows you to specify more than one definition for a function name or an operator in the
same scope, which is called function overloading and operator overloading respectively.
C++ allow us to create two or more functions with the same name and different parameters.
In this below program, function name sum is defined in two places with different parameters.
class Addition
{
public:
int sum(int num1,int num2) {
return num1+num2;
}
int sum(int num1,int num2, int num3) {
return num1+num2+num3;
}
};
void main()
{
Addition obj;
cout<<obj.sum(20, 15)<<endl;
cout<<obj.sum(81, 100, 10);
}
Explain about Operator Overloading in C++?
In C++, we can make operators to work for user defined classes.
Overloaded operator is used to perform operation on user-defined data type.
For example, we can overload an operator ‘+’ to concatenate two strings.
#include <iostream>
#include <conio.h>
using namespace std;
class example
{
int a,;
public:
void input()
{
cout<<"Enter the number - ";
cin>>a;
cout<<endl;
}
void operator -() //operator function as a member function
{
a=-a;
}
void display()
{
cout<<"a="<<a<<endl;
}
};
int main()
{
example e;
e.input();
cout<<"Before overloading unary minus operator"<<endl;
e.display();
-e;
cout<<"After overloading unary minus operator"<<endl;
e.display();
getch();
return 0;
}
Example:
float a=3.456
int j=a; (Here Variable a is in Float data type and Variable J is Integer data type.
So, Automatic conversion process convert the float into integer data type)