Wednesday, October 22, 2008

Console I/O Streams

There are three types of Console I/O Stream-

cout - Standard Output Stream
cin - Standard Input Stream
cerr - Standard Error Output Stream


cout :

It is used to display data on console.
Example :
cout<<"Sachin is a greatest Cricket Player"
cout<<234;
In general form :
cout< here expression can be any valid C++ expression

cin :

It is used to capture data from keyword.
Example :
int age;
cin>>age;
The general form :
cin>>variable;

cerr :

It is used to display errors on console.

C++ Keywords

Keywords are predefined reserved words (identifiers).They have special meanings. They cannot be used as identifiers in your program. The following keywords are reserved for C++.

* asm
* auto
* break
* case
* catch
* char
* class
* const
* continue
* default
* delete
* do
* double
* else
* enum
* explicit
* extern
* float
* for
* friend
* goto
* if
* inline
* int
* long
* mutable
* namespace
* new
* operator
* private
* protected
* public
* register
* return
* short
* signed
* sizeof
* static
* struct
* switch
* template
* this
* throw
* try
* typedef
* union
* unsigned
* using
* virtual
* void
* volatile
* while

Friday, October 17, 2008

My First Program

Now we will see how to write a program in C++. The idea of this program is to introduce you to the overall structure of a C++ program.

//My first program

#include<iostream.h>
int main()
{
//Print text message on console

cout<<"Hello Universe.. Give me Red ";

return 0;
}

Now after writing program in a text file(Source file) , save text file by name first.c

Note:

C++ source files conventionally use one of the suffixes .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx;

if you are using GNU Compiler, you can give following command to compile first.c

g++ first.c

To run
a.out

Output will be:
Hello Universe.. Give me Red


Example 2:

#include<iostream.h>
int main()
{
//Print multiple text messages on console

cout<<"Hello Universe.. Give me Red "<<endl;
cout<<"Congrats! Sachin ...";

return 0;
}

Thursday, October 16, 2008

Constructors & Destructors in C++

Constructor
Definition :
Constructor is a special member function with the same name as its class.

Example:
class Person
{

public:
Person(); // constructor for class Person
};

Use:
Constructors are used to create, and can initialize, objects of their class type.

Call:
When an object of a class is created, constructor for that class is called.

If there is no constructor defined, DEFAULT constructor is invoked.
But default constructor doesn't initialize.

Some facts about Constructors :
  • Constructors are special member functions with the same name as the class.
  • Constructors can not return any value (nothing even void) .
  • Constructors are intended to initialize the members of the class when an instance of that class is created.
  • Constructors are not called directly.
  • Constructors can not be virtual.
  • Constructors can not be static.
  • Constructors can not be const, volatile, or const volatile.
  • Constructors aren't automatically inherited between base and derived classes.
  • There can be any number of constructors in a same class. They must have different parameters to distinguish them. (=> Constructors can be overloaded )
Destructors
Definition :

Destructors in C++ also have the same name, but they are preceded by a '~' operator.

Example :
class Person {
public:
// Constructor for class Person
Person();
// Destructor for class Person
~Person();
};

Use :

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed.

Call:

The destructors are called when the object of a class goes out of scope or is explicitly deleted.

Some facts about Destructors:
  • If the constructor/destructor is declared as private, then the class cannot be instantiated. (why? Think about it.)
  • It is not necessary to declare a constructor or a destructor inside a class. If not declared, the compiler will automatically create a default one for each.
  • A destructor takes no arguments and has no return type.
  • Its address cannot be taken.
  • Destructors cannot be declared const, volatile, const volatile or static.
  • A destructor can be declared virtual or pure virtual.