Headlines News :
Home » , , » Lear C++ in 12 Days - Day 4

Lear C++ in 12 Days - Day 4

Written By Ente Malayalam on Friday, May 24, 2013 | 5/24/2013

DAY – 4
Till yesterday’s class we were concentrating on the procedural approach using C++. Now that we know the basics of C++ , lets move on to the object oriented approach using C++. Ie) now onwards we are going to stick on to the design rules which I already taught in our first class.
Let us consider the same Car dealer ship system as an example for implementation of the design steps and I will give few more examples to work in the lab.
            In the first class we have already done the abstraction of something known as dealer right. now lets try to do the   separation / encapsulation of the so called dealer.
Separation / Encapsulation:
            Encapsulation is achieved in C++ using classes and objects. To implement separation practically we need to know the syntax of classes and objects.
Syntax of a class is almost similar to the structure you have already studied. Or talking more specifically, I can say that syntax of a class is 99% similar to the syntax of a C++ structure.  Why I am specify C++ structure is , there is minor differences between C structure and C++ structure. First lets c that. Then we will move onto C++ structure and classes.
So tell me how you usually write a structure in C. The syntax is
struct struct_name       {                                 
//data members                                                                       
};
And usually u create a variable as
struct struct_name variable_name;
For eg:
struct student  {
            char Name[20];
            int Marks;
};
struct student anu;

Since data unit and function units are unrelated in C, the functions which are acting on this structure , for eg: Add_Details()  and Calculate_Percentage() are written as global functions. Ie) a C structure is dedicated to store data members only. There comes the major difference between C structure and a C++ structure. Even though the syntax is more or less same, a  C++ structure can have both data and functions. Ie) data and the functions which are acting upon that data can be combined together into a single unit. Keeping these things in mind, let’s try to wite the general syntax of structure in C++.
struct struct_name       {
            Access specifiers:
                        //data members & function members
            Access specifiers:
                        //data members & function members
};
The access specifiers mentioned in the syntax can be private, public or protected. In the first class itself I was telling you the purpose of encapsulation right. It is used to separate interface from implementation which in turn helps us to hide the data from the unnecessary intervention of outside functions. Or in other words through encapsulation along with the separation of interface and implementation, we achieve the flexibility of restricting access to the data members of the class only through the member functions of the class. This restricted access is provided using the access specifiers and classes.
            Let’s come to the functionality of these access specifiers. The specialty of private access specifiers is, the data or the functions defined inside this access specifier can be accessed only inside the class and that too only using the functions (usually known as the member functions) declared under the public access specifier of the class.
            The data or the functions declared in the public section of a class are visible to the outside world. Or the functions declared in the public access specifier acts as an interface between the user and the members in the private section of the class.
            For the time being just remember about these two access specifiers. We will come back to the protected access specifier in our later classes.
            Usually as a programming practice, the data members which have to be hidden from the outside world is put in the private section of the structure and function members acting on these data which in turn acts as an interface for the user will be defined in the public section of the class.

Another difference arises in the variable declaration for the structure. In C, struct keyword is a must in variable declaration where as in C++, variables can be declared as
struct_name variable_name;
So the above structure can be modified into C++ structure as
struct student  {
            private:
                        char name[20];
                        int marks;
            public:
                        void add_name()         {
                                    //body of function
                        }
                        void add_marks()        [
                                    //body of function
                        }
};
student anu;
There is one more difference between C and C++ structure. Have you ever tried writing an empty structure in C. of course it is possible even though it doesn’t have much purpose. Now the thing which is of interest to us is the size of the empty structure in C and C++. If you try it you can c that, the size of empty structure in C is 0 bytes and C++ is 1 byte. Very contradicting right? What can be the reason?
            Let’s take the definition of an empty structure. What ever be the language, an empty structure always refers to the structure with out any data members. In C, the only type of members you can have inside a structure is data members. So a structure with no data members doesn’t make much sense. So there is no meaning in wasting 1 byte of memory for a C structure. Where as in C++, even when the structure is empty or I should say, even when there are no data members inside my structure, there are chances that I have function members inside which can be accessed. To access these function members, we need to have a variable of a structure in our hands. So for a variable to come into existence, a minimum amount of memory is needed. The minimum amount of memory which can be given to a variable in our compiler is 1 byte. So this gives us the explanation for the behaviour of empty structures.
The same concept is extended to classes in C++. The general syntax of classes in C++ is
class class_name          {
            access specifiers:
                        //data members & function members
            access specifiers:
                        //data members & function members
};
class_name variable_name;
All the features of C++ structures including the size of empty structure is applicable to classes.
So naturally question arises. Y do we go for classes when we have structures which can do the same job. It is bcos of a very minor difference between C++ structures n classes. Even though the difference is minor, it is very crucial as far as object oriented languages are concerned.
            In C++ structures, data security is provided forcefully. Ie) unless and other wise u provide the private access specifier, by default the members of a C++ structure will be considered as public. But in classes, even if you don’t specify the access specifier, by default the members of a class will be considered as private. Ie) data security is assured automatically in C++ classes. So it is not a surprising thing why people go for classes rather than using structures right.
With these basic concepts, lets c how to implement encapsulation for the above mentioned dealer. Here the attributes we have traced out from the abstraction will be put under the private section of the class and the behaviour will be put in the public section of the class.
#include <iostream>
using namespace std;
class Employee
{
        private:
                char Employee_Name[20];
                int Employee_Id;
                int No_Cars_Sold;
                float Comm_Rate;
                float Comm_Car;
                float Total_Comm;
        public:
                void Add_Details(char *name, int id)
                {
                        strcpy(Employee_Name, name);
                        Employee_Id = id;
                        No_Cars_Sold = 0;
                        Comm_Rate = 0.16;
                        Comm_Car = Total_Comm = 0.0;
                }
                void Sell_Car(float Car_Price)
                {
                        No_Cars_Sold += 1;
                        Comm_Car = Car_Price * Comm_Rate;
                        Total_Comm += Comm_Car;
                }
                void Change_Comm(float Rate)
                {
                        Comm_Rate = Rate;
                }
                float Report_Comm()
                {
                        return Total_Comm;
                }
};
int main()
{
        Employee John;

        John.Add_Details("John Samuel", 1010);
        John.Sell_Car(200000.00);
        cout << John.Report_Comm() << endl;

        Employee Alex;

        Alex.Add_Details("Alex Mathew", 1011);

        Alex.Change_Comm(0.20);
        Alex.Sell_Car(200000.00);
        cout << Alex.Report_Comm() << endl;

        return 0;
}




Lets see the overall working of this program.
Here John and Alex are the two objects or instances I created for the class Employee. Add_Details(), Sell_Car() and Change_Comm_Rate() and Report_Income() acts as the interface here and the data and the definition of the above mentioned functions acts as the implementations.
 Immediately after creating the object ‘John’, first step which is going to happen internally is a block of memory is allocated equivalent to the size of the class (total size of data members). Since we have created local objects, the memory will be allocated in stack and will be initialized to garbage. Then object John invokes the function Add_Details() which initializes the allocated memory with the values passed. If you notice you can see that the object is associated with the function call with the dot operator other wise termed as class member access operator.
            In the same manner, when the object Alex is created, another block of memory corresponding to the class Employee is allocated which is initially having garbage. Then the same old procedures described above are repeated. The thing to be noted here is each and every object created will be having its own copies of data members present inside the class.
            Now lets talk a little more about these objects getting associated with the functions. Have you ever thought how these member functions identify which object is invoking the member function? As you know, these member functions don’t know anything about the name of the objects. So how is it achieved here.
Take an analogy situation. Lets consider two friends, Ramu and Somu. Lets say both of them are planning to construct a house based on the same plan and they are entrusting the job to the same contractor. Even though the plans are same ramu and somu’s specification of the house may be different. For eg: ramu may need his house to be built in brick, painted in white and flooring in tiles where as somu may need to build his house in stones, painted in blue and flooring in marble. Now if the contractor has to start his work, he needs to exactly know the correct address of the plot. Or in other words, ramu and somu should exactly tell the contractor about the address of the plot.
            Now correlate the above situation to our classes n objects. Here john n smith refers to ramu and somu and the member functions refer to the contractor. If the member functions have to work efficiently, it need to get the address of john n smith. Are we passing these address to this functions? no. even then your program works. Right. How? Its is working only because there is a person internally taking care of these address. If a person has to take care of the address, naturally it should be a pointer. Ofcourse it is a pointer n it is termed as this pointer.
            This pointer is an internal constant pointer associated with the class. When ever you invoke a function, immediately this pointer will be getting associated with that function and it will be holding the address of the object which is invoking the function. So this pointer is acting as an internal parameter to this function. The members handled inside the functions are equivalent to or internally represented as
this -> member
Other than this there are so many application for the this pointer. For eg: consider the following program.
//dealer.h
//Header file for Dealer in CDS
#include <iostream>
using namespace std;
class Dealer    {
        public:
                void Add_Dealer_Details(char*);
                void Sell_Car();
                void Change_Comm_Rate(float);
                float Report_Comm();
        private:
                char Name[20];
                int No_Cars_Sold;
                int Price;
                float Comm_Rate;
                float Comm_Per_Car;
                float Total_Comm;
};
//Dealer_Library.cpp
#include "dealer_header.h"
void Dealer :: Add_Dealer_Details(char *n)      {
        strcpy(Name, n);
        No_Cars_Sold = 0;
        Price = 200000;
        Comm_Rate = 0.16;
        Comm_Per_Car = Total_Comm = 0.0;
}
void Dealer :: Sell_Car()       {
        No_Cars_Sold += 1;
        Comm_Per_Car = Price * Comm_Rate;
        Total_Comm += Comm_Per_Car;
}
void Dealer :: Change_Comm_Rate(float Comm_Rate)        {
        Comm_Rate = Comm_Rate;
}
float Dealer :: Report_Comm()   {
        return Total_Comm;
}
#include "dealer_header.h"
int main()      {
        Dealer John;
        John.Add_Dealer_Details("Abraham John");
        John.Sell_Car();
        cout << John.Report_Comm() << endl;
        Dealer Smith;
        Smith.Add_Dealer_Details("Stephen Smith");
        Smith.Change_Comm_Rate(0.20);
        Smith.Sell_Car();
        cout << Smith.Report_Comm() << endl;
        return 0;
}
The only change I have made in the above program is, in the function Change_Comm_Rate(),  I have made the local variable name also as Comm_Rate. We were expecting the output as 32000 and 40000 right. but you can see that it prints us 32000 twice. This is because, in the your compiler is not able to resolve between the class member name and the local variable name. since local variables are having higher preference over global variables, in the string copying, both the arguments will be the local variable name where as your class member name is not initialized. So how to resolve this problem ?
            If we can explicitly tell our compiler that the first argument belongs to the class, the problem will be solved. And this can be done using our this pointer. So modifying our above program :
//dealer.h
//Header file for Dealer in CDS
#include <iostream>
using namespace std;
class Dealer    {
        public:
                void Add_Dealer_Details(char*);
                void Sell_Car();
                void Change_Comm_Rate(float);
                float Report_Comm();
        private:
                char Name[20];
                int No_Cars_Sold;
                int Price;
                float Comm_Rate;
                float Comm_Per_Car;
                float Total_Comm;
};


//Dealer_Library.cpp
#include "dealer_header.h"
void Dealer :: Add_Dealer_Details(char *n)      {
        strcpy(Name, n);
        No_Cars_Sold = 0;
        Price = 200000;
        Comm_Rate = 0.16;
        Comm_Per_Car = Total_Comm = 0.0;
}
void Dealer :: Sell_Car()       {
        No_Cars_Sold += 1;
        Comm_Per_Car = Price * Comm_Rate;
        Total_Comm += Comm_Per_Car;
}
void Dealer :: Change_Comm_Rate(float Comm_Rate)        {
        this -> Comm_Rate = Comm_Rate;
}
float Dealer :: Report_Comm()   {
        return Total_Comm;
}
#include "dealer_header.h"
int main()      {
        Dealer John;
        John.Add_Dealer_Details("Abraham John");
        John.Sell_Car();
        cout << John.Report_Comm() << endl;
        Dealer Smith;
        Smith.Add_Dealer_Details("Stephen Smith");
        Smith.Change_Comm_Rate(0.20);
        Smith.Sell_Car();
        cout << Smith.Report_Comm() << endl;
        return 0;
}
So when we put it as this -> name, it is like informing our compiler that the first argument name belongs to the object holded by the this pointer. So automatically problem will be solved.
Assignment :
            Concatenation of function call.
           
jus� ")' } ��� s� p class=MsoNormal style='text-align:justify'> 






//illustrating using directive

#include <iostream>

bool Check_Equal(int, int);

int main()
{
        using namespace std;

        int Val1, Val2;

        cout << "Enter the values to be compared" << endl;
        cin >> Val1 >> Val2;

        bool Result;

        Result = Check_Equal(Val1, Val2);

        if(Result)
                cout << "Both values are same" << endl;
        else
                cout << "Both values are not same" << endl;

        return 0;
}
bool Check_Equal(int V1, int V2)
{
        bool Check_Result =  V1 == V2;
}
















//illustrating using declaration

#include <iostream>

bool Check_Equal(int, int);

int main()
{
        using std :: cin;
        using std :: cout;
        using std :: endl;

        int Val1, Val2;

        cout << "Enter the values to be compared" << endl;
        cin >> Val1 >> Val2;

        bool Result;

        Result = Check_Equal(Val1, Val2);

        if(Result)
                cout << "Both values are same" << endl;
        else
                cout << "Both values are not same" << endl;

        return 0;
}
bool Check_Equal(int V1, int V2)
{
        bool Check_Result =  V1 == V2;
}

This is how you have to use header files in your program and the most commonly used among this is directive. The C++ designers have even modified the C header files by including namespaces. For eg: if you want to use stdio.h, remove the .h and add a c before stdio. Ie) cstdio. Even in this header file there is a namespace named as std. or I should say that the namespace std; is extended in multiple header files.

Now we know how to use our header files and write procedural like program in C++.


REFERENCE :

C++ Complete Reference : Herbert Schildt
C++ Primer Plus : Stephen Prata


Share this article :

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Maskolis | Johny Portal | Johny Magazine | Johny News | Johny Demosite
Copyright © 2011. Education World - All Rights Reserved
Template Modify by Creating Website Inspired Wordpress Hack
Proudly powered by Blogger