Вы находитесь на странице: 1из 7

Dr.S.J.M.

Yasin/CE206_lecture_08_class/ 28 March 2009/P-1

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-2

Object oriented programming / Class We can combine several variables and functions into a group which is called a class and give a name to it. We may define an object by use of a class. A class also gives us different levels of access control to its member variables and functions. A class may be considered as a user defined type. class name { access_specifier : variable ; -----------------access_specifier : function; -------} class box { public : double length; double width; double height; double volume( ){ return length*width*height; } }; class box { public : double length; double width; double height; double volume( ){ return length*width*height; } }; ----------------------------------------------------------double box::volume( ){ return length*width*height; }

There may be three types of access specifiers public private protected

Suppose we want to find the volume of a box. Here box is an object that we want to create in our code. We get the volume of a box from its three attributes length, width and height. Therefore, for our purpose of volume calculation we may define a box object by declaring a class. Here are some examples Note : default access specifier is private class box { private: class box { double length; double length; double width; double width; double height; double height; public: public: void getdata( ); void getdata( ); double volume( ); double volume( ); }; };

In these declarations class, public, private, void, double are keywords The body of a class is enclosed within braces { } and terminated by a semicolon As you can see there may be different sections in a class labeled by the access specifiers i.e. public, private or protected. (protected will be discussed later) Default specifier is private i.e. if a section is not explicitly labeled by an access specifier it means that section is private. Usually variables (also called data members or member variables) are placed in the private section and member functions (also called method or accessor method or class method) are placed in the public section. Definitions of the member functions can be provided within the class definition or outside the class definition

Once you define a class, then the class name becomes a type (something like int, double ). In the above examples we defined a class and named it box. Here box becomes a type name. Now we can declare an instance i.e. an object of box class. For example we can write box boxA; box a; (compare it with int num;) int num; memory is allocated for variable num memory is allocated for the member variables of the object boxA num

box boxA;

boxA

length width height

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-3

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-4

box wood;

memory is allocated for the member variables of the object wood

wood

length width height length width height

box paper;

memory is allocated for the member variables of the object paper

paper

In the above example the object box is a physical object. But there may be virtual objects such as an account.

class account { int account number; char[30] name ; double balance; public : void put_balance( ); void show_balance( ); double deposit( ); };

// Program - P18 // a simple class #include<iostream> using namespace std; class box { public: double length; double width; double height; double volume( ){ return length*width*height; } }; void main( ){ box boxA; cout<<"\n Length = ? "; cin>>boxA.length; cout<<"\n Width = ? "; cin>>boxA.width; cout<<"\n Height = ? "; cin>>boxA.height; cout<<"\n Volume = " <<boxA.volume() <<"\n\n"; }

Note : (program P18) Here we have defined a box class and have declared an instance boxA of box type . The member variables and member functions of the class box are declared public. So they are accessible in other function(s) such as main ( ) here. boxA.length refers to the length data member of the boxA object. Thus to access a data member of an object we make use of dot operator with the object name.

The data and function members of an object that are public can also be accessed from outside the object by pointer to object and arrow operator ( -> ). This will be discussed later.

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-5

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-6

In Program P18 the volume( ) function of the box class was defined within the class definition. We can also define the member functions outside the class definition using the scope resolution operator :: as shown in the following example. // Program - P21 // a simple class #include<iostream> using namespace std; class box { public: double length; double width; double height; double volume( ); }; void main( ){ box boxA; cout<<"\n Length = ? "; cin>>boxA.length; cout<<"\n Width = ? "; cin>>boxA.width; cout<<"\n Height = ? "; cin>>boxA.height; cout<<"\n Volume = " <<boxA.volume() <<"\n\n"; } double box::volume( ){ return length*width*height; } By now you should understand that a class-type variable has the property of binding the data and functions together. This property is referred to as encapsulation.

// Program - P19 // a simple example of a class #include<iostream> using namespace std; class box { double length; double width; double height; double volume( ){ return length*width*height; } }; void main( ){ box boxA; cout<<"\n Length = ? "; cin>>boxA.length; cout<<"\n Width = ? "; cin>>boxA.width; cout<<"\n Height = ? "; cin>>boxA.height; cout<<"\n Volume = " <<boxA.volume() <<"\n\n"; }

Program P19 is same as P18 except that no access specifier is mentioned in the definition of the box class. So the data members and function members of this box class have become private (default) and the data members are accessible within that class only. This program does not compile because boxA.length etc. now cannot be accessed from inside the function main( ). However, note that if all data and function members of a class are private then it is of no use because you wont be able to operate on the data members. So usually, data members of a class are kept private and function members are kept public. It is now understood, that by keeping the data members in the private section of a class, we can hide them from other functions (i.e. make them inaccessible in other functions). This is referred to as the data hiding capability of a class-type variable.

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-7

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-8

// Program - P20 /* data members are private and accessor methods are public*/ Note that in program #include<iostream> P20, data members of using namespace std; individual objects class box { boxA and boxB are private: accessed through double length; member function of the double width; object double height; public: void getdata(double l, double w, double h){ length = l; width = w; height = h; }; double volume( ){ return length*width*height; } }; void main( ){ box boxA; boxA.getdata(7.0, 5.0, 3.0); cout<<"\n\n Volume of boxA = " <<boxA.volume(); double side1, side2, side3; cout<<"\n\n side1 = ? "; cin>>side1; cout<<"\n\n side2 = ? "; cin>>side2; cout<<"\n\n side3 = ? "; cin>>side3; boxA.getdata(side1, side2, side3); cout<<"\n\n Volume of boxA = "<<boxA.volume(); box boxB; boxB.getdata(side1, side2, side3); cout<<"\n\n Volume of boxB = "<<boxB.volume()<<"\n\n"; }

Constructor A constructor is a member function in a class that has the same name as the class. It is special than other member functions of the class because it is executed when an instance of the class is defined. It provides the opportunity to initialize the new object as it is created, and to ensure that data members only contain valid values. Objects of classes in which the class contains a constructor cannot be initialized with a set of data values between braces. If you dont define a constructor for your class, the compiler will supply a default constructor. A constructor that has no parameter is called a default constructor. A constructor does not return a value, and therefore has no return type. Not even void.

// Program - P48 #include<iostream> using namespace std; class box { private: double length; double width; double height; public: box(double l, double w, double h){ length = l; width = w; height = h; }; double volume( ){ return length*width*height; } void display(){ cout<<" Volume = " <<volume()<<"\n\n"; } }; void main( ){ box boxA(7.0, 5.0, 3.0); cout<<"\nboxA : "; boxA.display(); double side1, side2, side3; cout<<"side1 = ? "; cin>>side1; cout<<"side2 = ? "; cin>>side2; cout<<"side3 = ? "; cin>>side3; box boxB(side1, side2, side3); cout<<"boxB : "; boxB.display(); }

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-9

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-10

Destructor

Inheritance We can create new classes by use of an existing one. The existing class is called base class and the new one is called derived class. The derived class may or may not have access to the data and function members of the base class. This is controlled by the rules of inheritance. Suppose we define the beam class objects as below. class beam { public: double span; void getspan(double s) { span = s; } }; Now we can define a class object for the concentrated load as below. class conc_load : public beam { private: double load; double distance; //distance from left support public: void getdata(double p, double x){ load = p; distance = x; } double reaction( ){ return load*(span-distance)/span; } };

Hear beam is a base class and conc_load is a derived class. It is a public derivation. As you can see, the conc_load class has a member function reaction that can access span from the beam object. We say that the derived class inherits from the base class.

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-11

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-12

Rules for class inheritance Base class member accessibility Accessibility in Derived class

Private Protected Public

Public derivation Not inherited Protected Public

Private derivation Not inherited Private Private

Protected derivation Not inherited Protected Protected

Next to define the concentrated load object (lets name it conc_load) we need to associate it with the beam object. We may derive the conc_class load class from the beam class (P30). Here we adopted a public derivation. If in the beam class, span is kept in the private section, we would not be able to get access to span in the conc_load class for the reaction() function. But making span a protected data member in the beam class we could achieve both data hiding and inheritance. #include<iostream> // Program - P30 using namespace std; class beam{ protected: double span; public: void getspan(double s){ span = s; } }; class conc_load : public beam { private: double load; double distance;//distance from left support public: void getdata(double p, double x){ load = p; distance = x; } double reaction( ){ return load*(span-distance)/span; } };

Multilevel inheritance A class may be declared with multi-level inheritance such as Class A{.}; Class B: public A {..}; // B derived from A Class C: public B {..}; // C derived from B

Multiple inheritance It is possible to derive a class with multiple base classes as shown below class A : access-specifier B1, access-specifier B2 { --------------------------------body of class A };

Suppose we want to write a program to calculate the shear force at any section of a beam subjected to a concentrated load. We may first define a beam object (lets name it beam). Obviously a data member of the beam object will be its span.

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-13

Dr.S.J.M.Yasin/CE206_lecture_08_class/ 28 March 2009/P-14

//rest of P30 void main( ){ double span_length, dist, point_load; cout<<"Span = ? "; cin>>span_length; cout<<"Distance of conc. load from left support = ? "; cin>>dist; cout<<"Conc. Load = ? "; cin>>point_load; conc_load p1; p1.getdata(point_load, dist); p1.getspan(span_length); for(int i=0; i<=span_length; i++){ if(i < dist) cout<<i<<"\t"<<p1.reaction()<<endl; else if(i == dist){ cout<<i<<"\t"<<p1.reaction()<<endl; cout<<i<<"\t"<<(p1.reaction() - point_load) <<endl; } else cout<<i<<"\t"<<p1.reaction()-point_load<<endl; } }

friend function We have mentioned it earlier that usually data members of a class are placed in the private section so they are not accessible from outside the class. However, there could be a situation where a function outside a class needs to access the private data of that class or two classes need to share a particular function. C++ provides an opportunity to handle such situations by declaring the function as a friend function. A friend function of a class can be a global function or it can be a member of another class. class ABC { .......... .............. public : .................. friend int XYZ(.........); ..................... } int XYZ(............){ ................... .................... } class X { ................................. double A(........); }; class Y { ............................... friend double X::B(........); }; double X::A(............){ ........................ ......................... }

Вам также может понравиться