CLASSES & OBJECTS
Classes and Objects, declaring a class, private members, protected members, public members, Example of a class
A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Classes are generally declared using the keyword class, with the following format:
class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
}object_names;
{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
}object_names;
Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.
An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire:
private members of a class are accessible only from within other members of the same class or from their friends.
protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
Finally, public members are accessible from anywhere where the object is visible.
protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
Finally, public members are accessible from anywhere where the object is visible.
By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access. For example:
class CRectangle
{
private :
int x, y;
public:
void set_values(int,int);
int area(void);
} rect;
{
private :
int x, y;
public:
void set_values(int,int);
int area(void);
} rect;
Declares a class (i.e., a type) called CRectangle and an object (i.e., a variable) of this class called rect. This class contains four members: two data members of type int (member x and member y) with private access and two member functions with public access: set_values() and area(), of which for now we have only included their declaration, not their definition.
Example of a class:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <conio.h>
#include <stdio.h>
class Employee
{
private: // Access specifier
int empcode; // Data member
char empname[20]; // . . . . . . . . . .
char empdesig[15]; // . . . . . . . . . .
float empsalary; // . . . . . . . . . .
float hra, da;
void cal_da(void) //Private member function not accessible outside class
{
cout << "\nDearness allowance : " << 1.2 * empsalary;
}
public:
void input_data(); // Member function prototype declaration
void display();
};
void Employee::input_data()
{
cout << "Enter the employee code ";
cin >> empcode;
cout << "Enter the employee name ";
gets(empname);
cout << "Enter the employee designation ";
gets(empdesig);
cout << "Basic Salary ";
cin >> empsalary;
}
void Employee::display()
{
cout << "\nEmployee code : " << empcode;
cout << "\nEmployee name : " << empname;
cout << "\nDesignation : " << empdesig;
cout << "\nBasic Salary : " << empsalary;
cal_da(); // Calling private function in public function print_data
}
{
private: // Access specifier
int empcode; // Data member
char empname[20]; // . . . . . . . . . .
char empdesig[15]; // . . . . . . . . . .
float empsalary; // . . . . . . . . . .
float hra, da;
void cal_da(void) //Private member function not accessible outside class
{
cout << "\nDearness allowance : " << 1.2 * empsalary;
}
public:
void input_data(); // Member function prototype declaration
void display();
};
void Employee::input_data()
{
cout << "Enter the employee code ";
cin >> empcode;
cout << "Enter the employee name ";
gets(empname);
cout << "Enter the employee designation ";
gets(empdesig);
cout << "Basic Salary ";
cin >> empsalary;
}
void Employee::display()
{
cout << "\nEmployee code : " << empcode;
cout << "\nEmployee name : " << empname;
cout << "\nDesignation : " << empdesig;
cout << "\nBasic Salary : " << empsalary;
cal_da(); // Calling private function in public function print_data
}
void main()
{
clrscr();
Employee emp; // Declares Object of type employee
emp.input_data();
cout << "Here is the employee information : \n";
emp.display();
}
{
clrscr();
Employee emp; // Declares Object of type employee
emp.input_data();
cout << "Here is the employee information : \n";
emp.display();
}
No comments:
Post a Comment