Pages

Sunday, 9 September 2012

Learn C++

Classes and Objects

Introduction

In previous module you have created basic programs using classes. This module enhances your learning to the classes in detailed. Apart from Classes this module also covers the topics constructor, copy constructor, destructor, function overloading and operator overloading.

Learning Objective

On successful completion of this module you should be able to:
  • Explain the purpose of a class constructor and identify a default constructor for a class
  • Explain the purpose of a class destructor and identify a default destructor for a class
  • Apply function overloading
  • Apply operator overloading
  • Code and use simple C++ classes using the following facilities:
    • public member functions and private data members
    • one or more constructors, including where appropriate, a default constructor
    • Overloading of functions
    • Operator overloading

The Class

In OOP, objects are instances of classes. What does this mean? Let’s look at an analogy. Almost all computer languages have built in data types like int, float, double, char etc… For example we take int. As you know you can declare as many variables of type int as you need in your program. <source lang = cpp> int age; int day; int count; </source>
In addition, class also provides security to the program data; there are three levels of Access Modifiers, which are associated with the class. The members (data or functions) can be declared to be:
  • Private members of the class are hidden and can be accessed only with in the class. Access to them members is denied from outside the class
  • Protected members are accessible within the class and to the derived class(to be covered in next modules)
  • Public members are visible outside the class and can be accessed from anywhere within the program.
A class looks just like a structure, and indeed they are almost equivalent. By convention we always use class for abstract data types and only use structure for ‘ordinary data structures’ that don’t have member functions. The only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.

The Class Scope

Every class defines the scope for the members in it. The data members of a class may be defined, before they are used in a member functions, or can be used in the member functions, and may be defined later in the class. The data members have the class scope, immaterial of where they are defined in the class.
Resources

Constructor

A constructor is a special member function that has the same name as the class. The constructor will be called automatically whenever an object is created. It could contain initializations to the variables of the object or calling other functions etc. The constructor by nature cannot return any value. The return has to be void. It can however take an argument list. The constructor which takes no arguments is called the default constructor. The constructor can be defined within the class definition or outside of it, just as any other member function definition. It can also be defined outside the class and made inline, just as other member functions.

Copy constructor

A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. First argument of such constructor is a reference to an object of the same type as being constructed (const or non-const), which might be followed by parameters of any type (all having default values). Normally the compiler automatically creates a copy constructor for each class (known as an implicit copy constructor) but for special cases the programmer creates the copy constructor, known as an explicit copy constructor. In such cases, the compiler doesn't create one.

Destructor

The destructor destroys a previously created object. The destructor takes no arguments and cannot return a value. The destructor has the same name as of the class, except that the name is preceded by a ~ (tilda).

Composite Class

A Composite object can be defined as one which consists of other objects. As an example, consider the class Circle as composite object containing an instance of Point, representing the centre and radius which is of type, float. The definition of Point and Circle in this case would be. <source lang="cpp">
class Point{ 
 float x;
 float y;
}; 
class Circle{ 
 float radius;
 Point centre;
}; </source>

Pass by value and Pass by reference

In the parameter passed to the copy constructor, you would notice an ampersand (&). Recall that in C, you can only pass parameters by value and not by reference. C++ enables you to pass parameters by reference. To do so, use an ampersand (&) before the name of the parameter in the parameter declaration.

Function Overloading

One really nice and cool feature in C++ over C is Function Overloading. This means that you can use the same name for different functions. Yes you could. For example, have three functions all named sum(), but each one would have a separate function definitions or return types. It may not seem obvious why you would want to do this, because you may need to sum two or more integers and floating type values in the same program. So these feature so helpful in that case.
Overloading constructor: When constructor is overloaded, the constructor must vary in the argument list. Upon object creation, that constructor for which there is match between the formal and the actual parameters is invoked.

Operator Overloading

You have seen how functions can be overloaded in C++, it also possible to overload the built in C++ operators such as +, -, >=, and ++ so that they could invoke different functions, depending on their operands. For example, the + in a+b will call a function if a and b are integers, but will call different function if a and b are strings or objects of a class you have created. Go through the given urls below to learn more about operator overloading.

The keyword this

The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the object itself. One of its uses can be to check if a parameter passed to a member function is the object itself.

No comments:

Post a Comment