Object and class Empty class The main reason
Object and class
Empty class • The main reason for using a class is to encapsulate data and code, it is how ever possible to have a class that has neither data nor code. class xyz { }; During the initial stage of development of a project, some of the classes are not fully identified or not fully implemented. Such emty classes are also known as stubs.
Friend function and friend class • Encapsulation and concept of data hiding gives us power of protecting the data form outside world. • If you are not a member you can not get it. • Suppose that a user wants a function to operate on object of two different classes.
• One of the convenient function of C++ allows nonmember function to access even the private member of a class. • The functions that are declared with keyword friend are called friend function. • A function can be friend to multiple classes. • The scope of a friend function is not limited to the class in which it has been declared. • It can not be called using the object of that class. • It can be invoked like a normal function. • It can be either declared as public or private.
class sample { int a, b; public: void setvalue() { a=25; b=30; } friend float avg(sample s); }; float avg( sample s) { return float(s. a+s. b)/2; } main() { sample p; p. setvalue(); cout<< the vag of two values=“<<avg(p); l }
• In the previous program the friend function accesses the class variable a and b by using the dot operator and the object passed to it.
• Member function of one class can be friend function of another class. The syntax is class x { The fun() is a member of ……. . class x and friend of class y. …………. int fun(); }; class y { friend int x: : fun(); };
class boy; class girl { int income; public: int girlfunc( boy b 1); void set( int in) { income=in; } Void show() { cout<<“ girl income”<<income; } }; Class boy { private: int income 1, income 2; public: void set( int in 1, int in 2) { income 1=in 1; Income 2=in 2; } Friend int girl: : girlfunc(boy b 1); }; Int girl: : girlfunc( boy b 1) { return b 1. income 1+ b 1. income 2; } Void main() { boy b 1; Girl g 1; b 1. set( 1000, 700); G 1. det(2000); Cout<<“ boy b 1 total income=“<<g 1. girlfunc(b 1);
Bridging classes with friend function class two; class one { int x 1; public: void set( int p) { x 1=p; } friend int add (one a, two b); }; Class two { int x 2; public: void set( int p) { x 2=p; } friend int add (one a, two b); }; int add ( one a, two b) { return a. x 1+ b. x 2; } void main() { one a; two b; a. set(10); b. set(15); cout<<sum of one and two=“<<add(a, b); }
• Friend int add( one a, two b); • this declaration can be placed either in public or the private sections of class. • Being a friend function it can access the private members of both classes. • class two; - it is necessary to declare this before class one. A class can not be referred until it has been declared.
Friend class • We can also declare all the member function of one class as the friend function of another class. The class is called friend class. • class z • { • friend class x • };
#include <iostream> using namespace std; class CSquare; class Crectangle { int width, height; public: int area () {return (width * height); } void convert (CSquare a); }; class CSquare { private: int side; public: void set_side (int a) {side=a; } friend class Crectangle; }; void CRectangle: : convert (CSquare a) { width = a. side; height = a. side; } int main () { CSquare sqr; CRectangle rect; sqr. set_side(4); rect. convert(sqr); cout << rect. area(); return 0; }
class employee { int bs, da, hra; public: void setdata(int a, int b, int c) { bs=a; da=b; hra=c; } friend class manager; }; • Class manager • { • int income; • public: • int access ( employee e) • { • return e. bs+e. da+e. hra; • } void setdata( int p) { income=p; } void show() { employee e 2; e 2. setdata(200, 150); cout<<employees basic sal is=“<<e 2. bs; Cout<<“manager sal is=“<<income; } }; Main() employee e 1; manager m 1; e 1. setdata(500, 300, 200); m 1. setdata(1000); cout<<“ employee total sal=“<<m 1. access(e 1); M 1. show(); }
Do it yourself • Create two classes DM and DB which stores the value of distances. DM stores distances in meters and centimeters, and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB use a friend function to carry out the addition operation. The object that stores the result may be a DM object or DB object. the display should be in the format of feet and inches or meter and cm.
Static data and member function • In some situation it is desirable to have one or more common data fields which are accessible to all objects of the class. • C++ allows static data and function which are common to all objects. • Syntax class Name { Static Data Type Data Member }; data type class name: : datamember =initial value;
Private static data member • Data hiding is achieved by declaring static data in private section. • As only the member functions can access to all the objects of class.
class test { static int count; int no; public: void set ( int n) { no=n; count ++; } void show() { cout<< the no. of calls made to set() through an object=“<< count; } }; Int test: : count=0; Void main() { test t 1; t 1. show() t 1. set(100); t 1. show(); test t 2, t 3; t 2. set(250); t 2. show(); t 2. set(300); t 3. show(); }
Access rules for static data members the public static data members can be accessed using the scope resolution operator or through objects with member access operator. The static data which is declared as public is similar to normal global variable. Class name: : variable name= value
Example class test { public: static int public_int; private: static int private_int; }; void main() { test: : public_int=240; t 1. public_int=370; }
- Slides: 19