
C++ CLASSES AND OBJECTS The data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in C programming language; In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects. The mechanism that allows you to combine data and the function in a single unit is called a class. Once a class is defined, you can declare variables of that type. A class variable is called object or instance. In other words, a class would be the data type, and an object would be the variable. Classes contain data known as members and member functions. HOW TO WRITE A CLASS: A class is specified with a name after the keyword class. The starting flower brace symbol '{'is placed at the beginning of the code. Following the flower brace symbol, the body of the class is defined with the member functions data. Then the class is closed with a flower brace symbol '}' and concluded with a colon ';'. There are different access specifiers for defining the data and functions present inside a class. Classes are generally declared using the keyword class, with the following format: class class_name { private: members1; protected: members2; public: members3; }; Where class_name is a valid identifier for the class. The body of the declaration can contain members, that can be either data or function declarations. The members of a class are classified into three categories: private, public, and protected. Private, protected, and public are reserved words and are called member access specifiers. Access specifiers: Access specifiers are used to identify access rights for the data and member functions of the class. There are three main types of access specifiers in C++ programming language: 1. Private 2. Public 3. Protected Private members of a class are accessible only from within other members of the same class. we cannot access it outside of the class. Protected members are accessible from members of their same class and also from members of their derived classes. Public members are accessible from anywhere where the object is visible A private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class. Public members are accessible from outside the class. A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class. When defining access specifiers, the programmer must use the keywords: private, public or protected when needed, followed by a semicolon and then define the data and member functions under it. Example : class student { private : int rollno; float marks; public: void getdata() { cout<<"Enter Roll Number : "; cin>>rollno; cout<<"Enter Marks : "; cin>>marks; } void displaydata() { cout<<"Roll number : "<<rollno<<"\nMarks : "<<marks; } }; 2.2 DECLARING OBJECTS Once a class is defined, you can declare objects of that type. The syntax for declaring a object is the same as that for declaring any other variable. The following statements declare two objects of type student: Student st1, st2; ACCESSING CLASS MEMBERS Once an object of a class is declared, it can access the public members of the class. st1.getdata(); THE public KEYWORD The keyword public can be used to allow object to access the member variables of class directly like structure. The public keyword is written inside the class. It is terminated by colon (:). The member variables and functions declared followed by the keyword public can be accessed directly by the object. The declaration can be done as below: Consider the Example. class item // class declaration { public: // public section begins int codeno; float prize; int qty; }; THE private KEYWORD The private keyword is used to prevent direct access to member variables or function by the object. The class by default produces this effect. The structure variables are by default public. To prevent member variables and functions of struct from direct access the private keyword is used. The syntax of private keyword is same as public. The private keyword is terminated by colon. Consider the Example. struct item { private: // private section begins int codeno; float prize; int qty; }; // end of class int main ( ) { clrscr( ); item one; // object declaration one.codeno=123; // member initialization one.price=123.45; one.qty=150; } Access Limits of Class Members Access specifiers Access permission Class Class object members public allowed allowed private allowed disallowed protected allowed disallowed 2.3 DEFINING MEMBER FUNCTIONS The member function must be declared inside the class. They can be defined in a) private or public section b) inside or outside the class. The member functions defined inside the class are treated as inline function. If the member function is small then it should be defined inside the class, otherwise it should be defined outside the class. If function is defined outside the class, its prototype declaration must be done inside the class. While defining the function, scope access operator and class name should precede the function name. The following program illustrates everything about member functions and how to access private member of the class. 2.3.1 Member Function inside the Class Member function inside the class can be declared in public or private section. The following program illustrates the use of member function inside the class in public section. 2.3.2 Private member function To execute private member function it must invoked by public member function of the same class. A member function of a class can invoke any other member function of its own class. This method of invoking other member function is known as nesting of member function. 2.3.3 Member Function outside the Class If a function is small is should be defined inside the class and if larger it must be defined outside the class. To define a function outside the class the following care should must be taken. The prototype of function must be declared inside the class. The function name must be preceded by name and its return type separated by scope access operator. Defining Member Function of Class Member functions defined inside a class this way are created as inline functions by default. It is also possible to declare a function within a class but define it elsewhere. Functions defined outside the class are not normally inline. When we define a function outside the class we cannot reference them (directly) outside of the class. In order to reference these, we use the scope resolution operator, ::(double colon). In this example, we are defining function getdata outside the class: void student :: getdata() { cout<<"Enter Roll Number : "; cin>>rollno; cout<<"Enter Marks : "; cin>>marks; } The following program demonstrates the general feature of classes. Member function initdata() is defined inside the class. Member funcitons getdata() and showdata() defined outside the class. class student //specify a class { private : int rollno; //class data members float marks; public: void initdata(int r, int m) { rollno=r; marks=m; } void getdata(); //member function to get data from user void showdata();// member function to show data }; void student :: getdata() { cout<<"Enter Roll Number : "; cin>>rollno; cout<<"Enter Marks : "; cin>>marks; } void student :: showdata() { cout<<"Roll number : "<<rollno<<"\nMarks : "<<marks; } int main() { student st1, st2; //define two objects of class student st1.initdata(5,78); //call member function to initialize st1.showdata(); st2.getdata(); //call member function to input data st2.showdata(); //call member function to display data return 0; } A class definition starts with the keyword class followed by the class name The class body, enclosed by a pair of curly braces A class definition must be followed either by a semicolon or a list of declarations. For example we defined the Box data type using the keyword class as follows: class Box { public: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; 2.3.4 CHARACTERISTICS OF MEMBER FUNCTIONS (1) The difference between member and normal function is that the former function can be invoked freely where as the latter function only by using an object of the same class. (2) The same function can be used in any number of classes. This is possible because the scope of the function is limited to their classes and cannot overlap one another. (3) The private data or private function can be accessed by public member function. Other functions have no access permission. (4) The member function can invoke one another without using any object or dot operator. 2.4 STATIC MEMBER VARIABLES AND FUNCTIONS (1) Static Member Variables We have noticed earlier that each object has its separate set of data member variables in memory. The member functions are created only once and all objects share the functions. No separate copy of function of each object is created in the memory like data member variables. It is possible to create common member variables like function using the static keyword. Once a data member variable is declared as static, only one copy of that member is created for the whole class. The static is a keyword used to preserve value of a variable. When a variable is declared as static it is initialized to zero. A static function or data element is only recognized inside the scope of the present class. Syntax: Static <variable definition>; Static <function definition>; The class and scope of the static member variables is defined outside the class declaration for the following reasons.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-