Friday, 27 May 2011

Constructors And Destructors



Constructors

The constructor's job is to set up the object so that it can be used. Before we initialize a variable, it stored a garbage value. We needed to initialize the variable to 0 or to some other useful value before using it. The same is true of objects. The difference is that with an object, you can't just assign it a value. You can't say:
Player greenHat = 0;
because that doesn't make sense. A player is not a number, so you can't just set it to 0. The way object initialization happens in C++ is that a special function, the constructor, is called when you instantiate an object. The constructor is a function whose name is the same as the object, with no return type (not even void). For our video game, we'll probably want to initialize our Players' attributes so that they don't contain garbage values. We might decide to write the constructor like this:
Player::Player() {
  strength = 10;
  agility = 10;
  health = 10;
}
We would also have to change the class declaration so that it looks like this:
class Player {
  int health;
  int strength;
  int agility;

  Player();  // constructor - no return type
  void move();
  void attackMonster();
  void getTreasure();
};


One problem with this constructor is that all of the players will be initialized to have strength=10, agility=10, and health=10. We might want to create players with different values for strength and agility to make our game more interesting. So, we can add a second constructor, which has parameters for strength and agility. Our class declaration would now look like this:
class Player {
  int health;
  int strength;
  int agility;

  Player();              // constructor - no return type
  Player(int s, int a);  // alternate constructor takes two parameters
  void move();
  void attackMonster();
  void getTreasure();
};
and we would add a function definition for the alternate constructor, which looks like this:
Player::Player(int s, int a) {
  strength = s;
  agility = a;
  health = 10;
}
Now, when we want to instantiate the Player object four times, we can do the following:
Player redHat;    // default constructor
Player blueHat(14, 7);  // alternate constructor
Player greenHat(6, 12);  // alternate constructor
Player yellowHat(10, 10);  // alternate constructor



Destructors

Destructors are less complicated than constructors. You don't call them explicitly (they are called automatically for you), and there's only one destructor for each object. The name of the destructor is the name of the class, preceeded by a tilde (~). Here's an example of a destructor:
Player::~Player() {
  strength = 0;
  agility = 0;
  health = 0;
}

Thursday, 26 May 2011

Program to execute multiple statements


//**********************
//program to execute multiple statements

#include <iostream.h>

void main ()
{
  cout << "Hello World!";
cout<<" I am a c++ programmer";
  getch();
}




We can use the "cout" expression to declare a line statement in c++. cout is declared in the iostream standard file, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.


Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).


 " // " is used to give a comment in C++.

My First C++ Program

Create your first C++ program and learn about its structure: the main() function and the statements


Let us start our tour of C++ with a simple program. Here is a simple program which outputs a line of text.



// This is my first C++ program
//It prints a line of text
# include <iostream>
void main()
{
cout << “My first C++ program ” <<endl;
getch();