Thursday, 4 July 2013

Classes Part - I

Classes - Part -I

The most important feature of C++ is the "Class". Its significance can be highlighted from the fact the Stroustrup initially gave it the name "C with classes". A class can be seen as an extension of the idea of structure which is used extensively in C. It is a new way of creating and implementing a user defined data type. Today we will discuss the concept of class by reviewing the traditional structures found in C and then the ways in which classes can be designed, implemented and applied.

Revisiting Structure from C

One of the unique features of C language is structures. They provide a method for packing together data of different types. A structure is a convenient tool for handling a group of logically related data items. It is a user defined data type with a template that serves to define its data properties. Once the structure type has been defined, we can create variables of that type using declarations that are similar to the built in type declarations, For example, in the following declaration :

struct student
{
      char name[20];
      int roll_number;
      float total_marks;
};

The keyword Struct declares student as a new data type that can hold three fields of different data types. These fields are known as structure members or elements. The identifier student, which is referred to as a structure name or structure tag, can be used to create variables of type student.

Structures can have arrays, pointers or structures as members.

Limitations of C Structure

The C language does not allow the struct data type to be treated like a built in data type. For example consider the following structure 

struct complex
{
       float x;
       float y;
}'
struct complex c1, c2, c3;

The complex numbers c1, c2 and c3 can be easily assigned values using the dot operand but we cannot add two complex numbers or subtract one from the other. For example :-

c1 = c2 + c3 ; is illegal in C.

Another important limitation of C structure is that they do not permit data hiding. Structure members can be directly accessed by the structure variables by any function anywhere in their scope. In other words, the structure members are public members.

Structures Extended in C++

C++ supports all the features of structures as present in C. But as is expected C++ has expanded the capabilities of structures further to suit its Object Oriented Programming philosophy. It brings the user defined types as close as possible to the built in data types, and also provide a facility to hide the data which is one of the main principles of OOP. Inheritance, a mechanism by which one data type can inherit characteristics from other data types is also supported by C++.

Some of the important features of Structures in C++
  • A structure can have both variables and functions as data members. 
  • Some of the data members can be declared as private which makes it impossible for external functions to access the data.
  • The keyword struct is not compulsory.
C++ incorporates all these changes in structures as a new user defined type known as Class. There is very little syntactical difference between structures and classes.

Generally in C++ we use structure for functions only holding data and use classes when we need to hold both data and functions. 

Note :- The only major difference between a structure and a class in C++ is that by default the members of a class are private, while in structure it is public by default.

We will learn more about classes in the next post. Keep visiting!

Sunday, 30 June 2013

Write a C++ Program to demonstrate function Overloading

//Program demonstrating the concept of function overloading//

//Author- Ashish Mishra//

//Date - 30/06//2013//

#include<iostream>

using namespace std;

//Prototype declarations

int volume (int);
double volume (double, int);
long volume (long, int, int);

int main()
{
cout<<volume(10)<<"\n";
cout<<volume(2.5,8)<<"\n";
cout<<volume(100L, 75,15)<<"\n";

return 0;
}

//Function definitions

int volume (int s)                //volume of a cube
{
return (s*s*s);
}

double volume ( double r, int h) //volume of a cylinder
{
return (3.14159 *r*r*h);
}

long volume ( long l, int b, int h)  //volume of rectangle
{
return (l*b*h);
}

The output of this program will be

1000
157.08
112500

Write a C++ Program to demonstrate default argument

//Program demonstrating the use of default argument//

//Author :- Ashish Mishra//

//Date :- 30/06/2013//

#include<iostream>

using namespace std;

int main()
{
float amount;

float value (float p, float n, float r = 0.15);     //Prototype declaration

        amount = value (5000.00 , 5);                //Default for the 3rd argument
  
cout<<"\n Final Value = "<<amount<<"\n";

return 0;

}

float value (float p, float n, float r)
{
int year = 1;
float sum = p;

while ( year <=n)
{
sum = sum*(1+r);
year= year + 1;

}

return (sum);
}

The output of the program is "Final Value = 10056.80"