[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
Java » userpage » VisualBasic » Software » TCPPSamples » CppVcl » QbasicFAQ_Print » Bandwidth » WhatLinksHere » talk:CCPlusPlus » CPP-Beginners-Tutorial-III

C++ Beginners Tutorial III

--------------------------------

This is part III of a series of beginners tutorials on C++. This part covers object oriented programming by using classes and templates.

Classes, Objects and methods

In object oriented programming classes, objects and methods are elementaray concepts that you need to understand to get started.

Classes

Classes are abstractions of real-world objects like, for example, a car. A car usually has the following elements to be considered as a car: it has tiers, a body, windows, doors, a steering wheel, etc.

In OOP terminology these would be called attributes or properties of an object. The object itself is the car. If we put the object and its properties together we have a new class. In this case, the name of the class would be car because it represents a car.

In C++, classes are customized data types. That is, they can be used the same way like elementary data types like int, char, double etc.

Our new class car may be implemented as follows:

class Car
{
  public:
    //basic constructor 
    Car(){};
     //methods of class car 
    int accelerate(int gears);
    int stop();
    void printInfo(Car crobj);
    //data elements 
  private:
    int speed;
    string name;
    int year;
        
};


This class already demonstrates all core concepts explained above. In OOP terminology, this is called an abstract class . Abstract classes implement real-world objects.

The constructor

The first obvious thing you may notice, is what looks like a function but without a return data type. This is called a constructor. In our class car, constructor the is called Car() because constructors always have the same name as their class.

The main job of a constructor is to create new instances of a class. Instances of a class are created as follows:

int main()
{
  //create a new instance of class Car
  Car myCar;
  //call method accelerate of class car
  myCar.accelerate(3);
  //call method stop of class car
  myCar.stop();
}


In the example above, a new object of our class Car is created by simply defining a new variable of the new data type Car. The next lines of code show how to use this new instance of our class with the . dot operator.

The next obvious thing you will notice are the public and private key words. They are essential when working with classes because they define the accessibility of the elements of a class.

The public key word for example defines that all elements are publicly available to other classes. The private key word defines, that the elements of a class are only visible or accessible for class members and may not be modified by other classes.

Besides public and private C++ supports also the protected key word.The folowing table explains the general rules for public, protected and private:

public    //can be usd and modified by other classes
protected //can only be used by derived classes, depending on the inheritance
private   //can only be used by its own class and friend classes


Methods

The car class also has defined 2 functions. In OOP terminology, these functions are called methods. Methods are called and manipulated by instances of the class.

Methods are implemented as follows:

class Car
{
  //method definitions
}
//defining constructor
Car::Car()
{
}
//defining methods
int Car::accelerate(int gear)
{
  gear = 6;
  return gear;
}
int Car::stop()
{
  gear = 0;
  return gear;
}
void Car::printInfo(Car crobj)
{
  cout<<"Manufacturer:\t"<<crojb.name<<"Build Year:\t"<<crobj.year<<endl;
}


Methods of classes are called by using the dot . operator.

int main()
{
  Car myCar;
  myCar.accelerate(3);
  myCar.stop();
 
}


This example equals the one I have used above when I introduced constructors.

last edited (December 24, 2006) by bilderbikkel, Number of views: 28814, Current Rev: 15 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Site Management by Lars Hagelin at Kontantkort.se.