Classes Chapter 4 Classes and Objects Data Hiding and Encapsulation Function in a Class Using Objects Static Class members
Classes Class represents a group of Similar objects A class is a way to bind the data describing an entity and its associated function together. Consider an account having characteristics Account no., type, balance Its associated operations are DEPOSITE and WITHDRAWAL.
Class Definition (Construct) class account { int acno; int type; float balance; float deposit( float amt) { balance = balance + amt; } float withdrawal (float amt) { balance = balance amt; } };
Why class are needed? In the software field of Information Technology Industry, a need was felt to create software to real world object. Real world objects certainly have data- type properties such as account no. balance etc. But real world object also can also do certain things like deposit and withdrawal. Now to combine all these properties of real world objects, one needed a way out. The structure of C could provide a half way help by binding the data type properties but still the operation association left out.
Declaration of Class The declaration of class involves declaration of its four associated attributes: Data members are the data type properties that describe the characteristics of a class. There may be zero or more data members in a class Member function are the set of operations that may be applied to objects of that class. There may be zero or more member function for a class. They are referred as class interface.
Continue Program Access Level that control access to member from within the program. These access levels are PRIVATE, PROTECTED and PUBLIC. Depending upon the access level of a class member access to it is allowed or denied. Class tagname that serves as a type specifier for the class using which objects of this class type can be created.
Structure of Class class classname { private: private members and function visible only inside the class protected:. protected members and function visible class and his child classes public:.. Visible in any where in the program }
Class Method Definition Methods (Member Function) can be defined from two ways Inside the class Outside the class.
Inside the class - Method Definition Methods of class often define in public section. class name { private:.. Data member; public: return-type type method_name (paramete list) {.. Function Body } };
Outside the class - Method Definition return-type type class_name :: method_name (Parameter list) {.. Function body } Here :: pronounced as Scope Resolution Operator
Difference between struct and class In struct all members are public but in class all members by default be private. In struct there is no methods but class hold members as well as methods. struct can not derived any other struct, but class can derive any other class.
Array within the class A class can have an array as its member variable. An array in the class can be private or public data member of the class. class exarray { int arr[10]; public: int largest(); int sum(); };
Scope of Class and its member Public members are the members that can access directly by any function. Whether member function of the class or non member function Private Member are the class members that are hidden from the outside world. The private member of the class can be used by the member function of the class
Scope Rules and Class Global Class A class is said to be global class if its definition occurs outside the bodies of all functions in a program. Which means that object of this class type can be declared from any where in the program. Local Class A class is said to be local if its definition occurs inside a function body. Which means that object of this class can be declared only within the function.
Example #include<iostream.h> void main() { // main started class y { // class started }; // class end } // main end
Scope for Objects Global Object An object is said to be global Object if it is declared outside all the function bodies and it means that this object is globally available to all function in the program. This object can be used any where in the program.
Local Object An object is said to be local if it is declared within a function, which means that this object is locally available to the function that declares it and it cannot be used outside the function declaring it.
Example #include<iostream.h> class X {.. }; X obj1,obj2; // void main() { X obj3,obj4; } // Global object; //Local object
Scope of Private and Protected member The PRIVATE and PROTECTED members of a class have class scope. It means these can be accessed only by the member function of the same class. These members can not be accessed directly by using the object names. In other words nowhere except the bodies of member function can be names of private and protected member appear.
Scope of Public members The scope of the public members depends upon the object being used for referencing them. If the referencing object is a global object, then the scope public members is local.
#include<iosream.h void main() void fun1() { class x { private : int a; { x obj2; x obj3; void fc() { cout<<a; } public: int i; void fcc() { cout<<2*i; A=13; obj1.i=10; obj1.fcc(); obj2.i=20; obj2.fcc(); } obj1.i=12; obj1.fcc(); obj2.i=25 obj2.fcc(); obj3.i=30; obj3.fcc(); Valid Valid Invalid Invalid Valid Valid fc();}}; } x obj1;
Types of Class Function Accessor Function Mutator Function Manager Function
Accessor Function These Function are those which can not change the data members value, but they simply use for the retrieving the value of data members; It means, Accessor function are used to read values of private data members of a class which are directly not accessible in non member function.
class student { int rollno; float marks; char name[25]; char grade; public: void readdata() { } void printdata() { } int getrollno() { return(rollno); } float getmarks() { return(marks); } };
Mutator Function This type of function are used to manipulate the value of private data members. Generally most of the member function are such type.
class student { int rollno; float marks; char name[25]; char grade; public: void readdata() { } void printdata() { } void calculate() { if marks>=90 grade='a'; else if marks>=75 grade='b'; else if marks>=60 grade='c'; else grade='d'; }
Manager Function These are the member function with specific type. This types of function are also CONSTRUCTOR and DESTRUCTOR. These function are used to initialize and destroying the objects.
Nested Class A class is declared within another class. A class is declared within another class is called nested class. The outer class is class is called enclosing class and inner class is called the nested class.
Member Access Control of Nested Class The member function of nested classes have no special access to members of an enclosing class. They obey the usual access rule and the same is true for enclosing class for the access of nested class members Nested class can access public member of enclosing by own object and similarly enclosing class can access inner class public member by own object. Private and Protected member is not available for other class.
Inline Function The inline function are a C++ enhancement designed to speed up programs. The coding of normal function and inline function is similar except that inline functions definition starts with the keyword inline. The distinction between normal function and inline function is the different compilation process for them.
Original Program void main {...... square(3);...... square(3);...... square(3); } void square(int i) { cout<<i*i; } Compiled Program void main {...... { cout<<3*3; }...... { cout<<3*3; }...... { cout<<3*3; } }
When we make function inline The function should be inline only when they are small The function which does not contains return value, loop, switch and goto statement. The function which does not contains static variables. The function should not recursive.
Constant Member Function If a member function of a class does not alter any data in the class. This member function may be declared as constant member function using the keyword const Example. int max (int, int) const; void prn(void) const;
Use of :: Operator int x,y; class A { public: int x; void f(int x) { x=i; ::x=i; y=i; } };
Array of Object C++ Supports arrays of any data type including class type. An array having class type elements is known as Array of Object. Array of Object is declared after the class definition is over and it is defined in the same way as other array is defined.
Object as Function Arguments Just like any other data types of C++, an object is also passed as function argument. An Object can also be passed as Call by Value and Call by Reference An Object can be passed to any member function, non member function and any friend function.
Function Returning Object An object can not only be passed to the function but a function can also return an object. In other words, A function can also be a class type. student total (student); distance add (distance d, distance e) { distance k; --- --- Return(k); }
Static Data Members Static data members of a class just like a global variable for its class. The static data members are stores value common to the entire class.
Difference between static and simple data members There is only one copy of the data members of a class is maintained for the entire class, which is shared by all the object of the class. It is visible only within the class. However its life time (the time for which it remain in memory) is the entire class.
How to make static data member Declaration within the class class X { static int count; }; Definition outside the class Type ClassName :: DataMember = value; int X :: count = 0; A static data member must be defined outside the the class definition.
Initialization of const Static data Members const data members of standard data type now can be initialized inside the class. Initialization is also a type of definition, so no further definition is required outside the class. class demo { static const int amount =400; static const float val =50; };
Static Member Function A static member function that accesses only the static data members of a class may be declared as static. This can be done by putting the keyword static before the function declaration in the class definition. class demo { static int a; public: static void show() {... } };
Difference between static member function and other member function A static member function can access only static data members of the same class, But other member function can access both type of data member.. A static member function can invoked by using the class name instead of object of class like classname :: staticfunctionname()