You are on page 1of 7


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;


length width height

.Yasin/CE206_lecture_08_class/ 28 March 2009/P-4 box wood.M.J. double volume( ){ return length*width*height.Dr. double deposit( ). The member variables and member functions of the class box are declared public. public : void put_balance( ). } Note : (program P18) Here we have defined a box class and have declared an instance boxA of box type . }. cout<<"\n Width = ? ".width. cout<<"\n Height = ? ". memory is allocated for the member variables of the object wood wood length width height length width height box paper. void main( ){ box boxA. char[30] name . class box { public: double length. memory is allocated for the member variables of the object paper paper In the above example the object box is a physical object. boxA.P18 // a simple class #include<iostream> using namespace std. double width. double balance. cout<<"\n Length = ? ". But there may be virtual objects such as an account.S. This will be discussed later. double height. cin>>boxA.Yasin/CE206_lecture_08_class/ 28 March 2009/P-3 Dr.M. class account { int account number. 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 ( -> ).height. } }.S. void show_balance( ).volume() <<"\n\n". cin>>boxA. Thus to access a data member of an object we make use of dot operator with the object name.length. // Program .length refers to the length data member of the boxA object. cout<<"\n Volume = " <<boxA.J. cin>>boxA. So they are accessible in other function(s) such as main ( ) here.

length etc. note that if all data and function members of a class are private then it is of no use because you won’t be able to operate on the data members. We can also define the member functions outside the class definition using the scope resolution operator :: as shown in the following example. cin>>boxA. we can hide them from other functions (i. } Program P19 is same as P18 except that no access specifier is mentioned in the definition of the box class. This is referred to as the data hiding capability of a class-type variable. cout<<"\n Length = ? ".e. 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. } }. now cannot be accessed from inside the function main( ).S.P19 // a simple example of a class #include<iostream> using namespace std. // Program .M.M. class box { double length. However. } By now you should understand that a class-type variable has the property of binding the data and functions together. void main( ){ box boxA. class box { public: double length. cout<<"\n Height = ? ". cin>>boxA.volume() <<"\n\n".Yasin/CE206_lecture_08_class/ 28 March 2009/P-5 Dr.length. double volume( ){ return length*width*height. So usually. make them inaccessible in other functions).J. cin>>boxA.width.length. }. This program does not compile because boxA.P21 // a simple class #include<iostream> using namespace std. cout<<"\n Width = ? ".S. cin>>boxA. cin>>boxA. double height. cin>>boxA. double volume( ). void main( ){ box boxA. } double box::volume( ){ return length*width*height. that by keeping the data members in the private section of a class.width. This property is referred to as encapsulation.height.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. double width. cout<<"\n Volume = " <<boxA. cout<<"\n Volume = " <<boxA. cout<<"\n Width = ? ".Dr. double height.height.volume() <<"\n\n". cout<<"\n Height = ? ". data members of a class are kept private and function members are kept public. It is now understood. cout<<"\n Length = ? ". // Program . double width.J. .

volume(). double side1. class box { private: double length. double w. height = h.volume(). cin>>side3. data members of using namespace std. double volume( ){ return length*width*height. the compiler will supply a default constructor. }.Dr. boxA.display(). side2. side2. 5. It is special than other member functions of the class because it is executed when an instance of the class is defined. width = w. cout<<"\n\n Volume of boxA = "<<boxA. It provides the opportunity to initialize the new object as it is created. side3). side3. cin>>side2. cin>>side1. public: void getdata(double l.S. Not even void.getdata(side1. side2. double h){ length = l. double side1.Yasin/CE206_lecture_08_class/ 28 March 2009/P-8 // Program . cin>>side2. If you don’t define a constructor for your class. 5. double height. } }. object double height.0.J. cin>>side3. double width. A constructor does not return a value. box boxB.M. member function of the double width. cout<<"side1 = ? ".0. cin>>side1. } }. double volume( ){ return length*width*height. boxB.getdata(7.0). side2. 3. height = h. side2. individual objects class box { boxA and boxB are private: accessed through double length. cout<<"side3 = ? ".J. cout<<"boxB : ".S. and therefore has no return type.volume()<<"\n\n". side3). } void display(){ cout<<" Volume = " <<volume()<<"\n\n".P48 #include<iostream> using namespace std. boxA. boxA. double h){ length = l. boxB. A constructor that has no parameter is called a default constructor. side3).getdata(side1. side3.0.P20 /* data members are private and accessor methods are public*/ Note that in program #include<iostream> P20. box boxB(side1.0). void main( ){ box boxA. } Constructor A constructor is a member function in a class that has the same name as the class. // Program . double w. }. cout<<"\n\n Volume of boxA = " <<boxA.0. cout<<"\nboxA : ". cout<<"\n\n side2 = ? ".M. cout<<"\n\n side3 = ? ". } . width = w. cout<<"\n\n Volume of boxB = "<<boxB.display(). 3.Yasin/CE206_lecture_08_class/ 28 March 2009/P-7 Dr. public: box(double l. void main( ){ box boxA(7. Objects of classes in which the class contains a constructor cannot be initialized with a set of data values between braces. cout<<"side2 = ? ". cout<<"\n\n side1 = ? ". and to ensure that data members only contain valid values.

Yasin/CE206_lecture_08_class/ 28 March 2009/P-9 Dr. class conc_load : public beam { private: double load. It is a public derivation.Yasin/CE206_lecture_08_class/ 28 March 2009/P-10 Destructor Inheritance We can create new classes by use of an existing one. 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.Dr. distance = x. Hear beam is a base class and conc_load is a derived class.S. Suppose we define the beam class objects as below. } }. We say that the derived class inherits from the base class.S. Now we can define a class object for the concentrated load as below. the conc_load class has a member function reaction that can access span from the beam object.M. double distance. As you can see. //distance from left support public: void getdata(double p. class beam { public: double span.J. void getspan(double s) { span = s. . The existing class is called base class and the new one is called derived class.J.M. double x){ load = p. } }. } double reaction( ){ return load*(span-distance)/span.

class conc_load : public beam { private: double load. // 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. Class B: public A {……. Here we adopted a public derivation.}. class beam{ protected: double span.Yasin/CE206_lecture_08_class/ 28 March 2009/P-11 Dr. #include<iostream> // Program . double distance.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.J.M.Dr. 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.P30 using namespace std. } }.//distance from left support public: void getdata(double p. access-specifier B2 { --------------------------------body of class A }.S. } double reaction( ){ return load*(span-distance)/span. // B derived from A Class C: public B {…….M. } }. public: void getspan(double s){ span = s. Multilevel inheritance A class may be declared with multi-level inheritance such as 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. But making span a protected data member in the beam class we could achieve both data hiding and inheritance.. Obviously a data member of the beam object will be its span.}.}. distance = x. If in the beam class. double x){ load = p.S. We may derive the conc_class load class from the beam class (P30). We may first define a beam object (lets name it beam).. .J.

.... public : .. } } 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............getdata(point_load.............J. ..getspan(span_length). cout<<"Conc..reaction()<<endl......... }.. ...){ . dist)... ....). friend int XYZ(... for(int i=0........ class ABC { ...... cin>>span_length........... }. } else cout<<i<<"\t"<<p1... dist.......Yasin/CE206_lecture_08_class/ 28 March 2009/P-13 Dr...........).Yasin/CE206_lecture_08_class/ 28 March 2009/P-14 //rest of P30 void main( ){ double span_length....... cout<<"Span = ? ". friend double X::B(.... Load = ? "........ 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...... double A(........... cin>>dist..){ ............reaction()-point_load<<endl.. cout<<i<<"\t"<<(p1...... class Y { ....reaction()<<endl.... cin>>point_load.point_load) <<endl.......S...reaction() ... } ........S.. cout<<"Distance of conc. point_load...... A friend function of a class can be a global function or it can be a member of another class...... ......... conc_load p1.......... double X::A(............... p1.....).. } int XYZ(.... else if(i == dist){ cout<<i<<"\t"<<p1..... i++){ if(i < dist) cout<<i<<"\t"<<p1.. load from left support = ? "... C++ provides an opportunity to handle such situations by declaring the function as a friend function..M....... p1. } class X { ...........J....Dr.. i<=span_length......M.......