C++ Classes
- Class binds the data and its associated functions together.
- It allows data hiding from external use.
- The general form of class declaration is:
class class_name { private: data_type data1; ......... public: return_type function_name(data_type arg_list); ............. };
Access Specifiers
- There are 3 access specifiers : private, public and protected.
- It determines the visibility level of class members.
- Members declared as private can be accessed only from within the class.
- Public members are visible from outside the class also.
- Protected members are visible by member function and friend function.
- By default, the members of a class are private.
- Variables declared inside a class is data members.
- Functions declared inside a class is member functions.
Object and Member Access
- Once a class has been declared, it is possible to create variables of its type, known as objects of a class.
- Syntax : class_name obj_name1, obj_name2, .........;
- The necessary memory space is allocated when object is created.
- The public members of a class can be accessed by making use of member access operator or dot operator i.e '.'
- Syntax:
class_object . data_member;
class_object . member_function (actual_arguments);
Defining Member Functions
- The member functions can be defined inside or outside the class definition.
Inside Class Definition
- The functions defined inside the class is treated as inline function.
#include
class employee { char name[30]; float salary; public: void getdata() { cout << "Enter name and salary : "; cin >> name >> salary; } void showdata() { cout << name << salary << endl; } };
int main() { employee *eptr; employee e; eptr = &e;
eptr -> getdata(); eptr -> showdata(); return 0; }
Outside Class Definition
- The function declaration must be inside the class definition.
- The function defined outside the class should be bound with the class to which it belongs by the use of scope resolution operator '::'
#include
class employee { char name[30]; float salary; public: void getdata(); void showdata(); };
void employee :: getdata() { cout << "Enter name and salary : "; cin >> name >> salary; } void employee :: showdata() { cout << name << salary << endl; }
int main() { employee *eptr; employee e; eptr = &e;
eptr -> getdata(); eptr -> showdata(); return 0; }
Constructors
Constructor
- Constructor is a special member function which is automatically called during object creation.
- Its name is same as the name of the class.
- It has no return value specification.
Default Constructor and Parameterized Constructor
- Default constructor takes no arguments.
- It is automatically called when no arguments are supplied.
- Parameterized constructor is the constructor with parameters.
Illustration of default and parameterized constructors
class counter { private: int count; public: // Default Constructor counter() { count = 0; }
// Parameterized Constructor
counter(int n)
{
count = n;
}
.............
.............
};
int main()
{
counter c1;
counter c2(5);
}
Copy Constructor
- It declares and initializes an object from another object of its own class.
- When no copy constructor is defined, the compiler supplies its own copy constructor known as default constructor.
Illustration of Copy constructor
class code { int id; public: code() {} // Default constructor code (int a) // Parameterized constructor { id = a; } code(code &a) //Copy constructor { id = a.id; } void display() { cout << id; } };
int main()
{
code a(100);
code b(a); //Calling copy constructor
code c = a; //Calling copy constructor
a.display();
b.display();
c.display();
return 0;
}
Destructors
- It is a member function whose name is same as the class name but is preceded by a tide (~).
- It is invoked when an object is destroyed.
- It never takes any argument nor any return value.
class test { private: ........... public: test() { } //Constructor ~test() { } //Destructor };
Object as Function Argument and Return Type
Object as Function Arguments
- The objects can be passed to the function as an arguments.
#include
class complex { float real, img; public: void getdata() { cout << "Enter real and imaginary part"; cin >> real >> img; } void showdata() { cout << real << "+i" << img; } void add(complex c1, complex c2) { real = c1.real + c2.real; img = c2.img + c2.img; } };
int main() { complex c1, c2, c3; c1.getdata(); c2.getdata(); c3.add(c1, c2); c1.showdata(); c2.showdata(); c3.showdata(); return 0; }
Object as Return Type
#include
class complex { float real, img; public: void getdata() { cout << "Enter real and imaginary part"; cin >> real >> img; } void showdata() { cout << real << "+i" << img; } complex add(complex c2) { complex result; result.real = real + c2.real; result.img = img + c2.img; return result; } };
int main() { complex c1, c2, c3; c1.getdata(); c2.getdata(); c3 = c1.add(c2); c1.showdata(); c2.showdata(); c3.showdata(); return 0; }
Array of Objects
class employee { private: int emp_id; char name[30]; public: void getdata(); void showdata(); };
//Declaring array of objects
employee emp[50];
Pointer to Objects and Member Access
- The object pointer variable holds the address of the objects.
- The starting address of an object can be achieved by address operator.
- The declaration of pointer to object is:
class_name *pointer_to_object; class_name object_name; pointer_to_object = &object_name;
- The members can be accessed as:
pointer_to_object -> member;
OR,
(*pointer_to_object).member;
Illustration of Pointers in C++
#include
class employee { char name[30]; float salary; public: void getdata() { cout << "Enter name and salary : "; cin >> name >> salary; } void showdata() { cout << name << salary << endl; } };
int main() { employee *eptr; employee e; eptr = &e;
eptr -> getdata(); eptr -> showdata(); return 0; }
DMA for Objects and Object Array
Syntax
class_name *obj_ptr;
obj_ptr = new class_name; //Dynamic allocation for object
obj_ptr = new class_name[size]; //Dynamic allocation for object array
Illustration of DMA in C++
class test { private: int data; ........... public: test() { } test(int a) { data = a; } };
test *tptr, *taptr;
tptr = new test;
taptr = new test[a];
this Pointer
- Every non-static member function of object have access to 'this' pointer, which points to the object itself.
- It is defined implicitly.
- Any non-static function can find out the address of the object of which it is a member of.
Illustration of 'this' pointer
class counter { unsigned int count; public: counter() { count = 0; } counter(int n) { count = n; } counter operator ++() { ++count; return *this; } };
Static Data Member and Static Function
Static Data Member
- Storage space for static data members are not allocated when the object is created.
- Static data members of all objects are stored in the same memory locations.
- It is declared in the class and is defined outside the class.
- It is declared by using keyword 'static'.
Illustration of Static Data
#include
class item { static int count; //Static Data Member Declaration int number; public: void getdata(int a) { number = a; count++; } void getcount() { cout << "Count = " << count; } };
int item :: count = 0; //Static Data Definition
int main() { item a, b; a.getcount(); b.getcount(); a.getdata(100); b.getdata(200); a.getcount(); b.getcount(); return 0; }
Output: count = 0 count = 0 count = 2 count = 2
Static Function
- It belongs to a class not to any object.
- It can access only the static data members.
Illustration of Static Function
#include
class item { int code; static int count; public: void setcode() { code = ++count * 10; } void showcode() { cout << code; } static void showcount() //Static Function Definition { count << count; } };
int item :: count = 0; //Static Data Definition
int main() { item t1, t2; t1.setcode(); t2.setcode(); test :: showcount(); // Calling static function return 0; }
Constant Member Function and Constant Objects
Constant Data Member
- The value of constant data members can not be changed by any function members.
- Initialization can be done only by the constructors.
Illustration of Constant Data Member
#include
class con { const int constdata; //Declaration of constant data member public: con() : constdata(0) {} //Definition of constant data through constructor void display(); };
void con :: display() { cout << constdata <}
Constant Member Function
- The functions which guarantee not to change object's value are called constant functions.
- Constant objects can only call constant functions.
Illustration of Constant Member Function
class time { private: int hr, min, sec; public: time(int h = 0, int m = 0, int s = 0) { hr = h; min = m; sec = s; } int hour() const //Definition for constant functions { return hr; } int minute() const { return min; } int second() const { return sec; } };
Friend Function and Friend Class
Friend Function
- Using friend function, non-member of a class can access the member of a class.
- A friend function is not a member of any classes but has the full access to the member of class where it is declared as friend.
- It can be invoked like a normal function without help of any object.
- It can not access member name directly and has to use an object name and dot operator with each member name.
- It takes object as arguments.
Illustration of Friend Function
#include
class complex { private: float real, img; friend complex add(complex, complex); //Friend function declaration public: void getvalue() { cout << "Enter real and imaginary part : "; cin >> real >> img; } void showvalue() { cout << real << " + i" << img; } };
complex add(complex c1, complex c2) { complex temp; temp.real = c1.real + c2.real; temp.img = c1.img + c2.img; return temp; }
int main() { complex c1, c2, c3; c1.getvalue(); c2.getvalue(); c3 = add(c1, c2); c3.showvalue(); return 0; }
Friend Class
- Any function that is a member of another class can also be a friend of one class.
Illustration of Friend Class
#include
class B;
class A { char passA[20]; public: void getdata() { cout << "Enter password of A : "; cin >> passA; } void showdata(B); friend class B; };
class B { char passB[20]; public: void getdata() { cout << "Enter password of B : "; cin >> passB; } void showdata(A a) { cout << "Password of A is : " << a.passA; } friend class A; };
void A :: showdata(B b) { cout << "Password of B is : " << b.passB; }
int main() { A a; B b; a.getdata(); b.getdata(); a.showdata(b); b.showdata(a); return 0; }
- by SURAJ AWAL
Ⓒ Copyright ESign Technology 2019. A Product of ESign Technology. All Rights Reserved.