Beruflich Dokumente
Kultur Dokumente
Pointers in C/C++
Pointers are variables that contain memory addresses (see Addresses, Pointers, and
References). They are an essential data type in C and C++, and are used for the following:
Array variables are pointers and pointers can be used as alternative to subscripts.
Dynamic memory allocation/deallocation returns a pointer to the allocated memory.
For efficiency by passing the address of an object instead of copying the object to a
function. Reference parameters typically used.
For function parameters that will be changed by the function
(out or inout parameters). Reference parameters typically used.
Declaring a pointer
Pointers are declared to point to a particular datatype. A "*" is written after the type to
indicate that this is a pointer to that type. For example, a pointer to an int would be
declared like this.
int* ip; // declares ip to be a pointer to an int.
You will also see variations in the spacing such as
int *ip;
int * ip;
NULL
NULL is the pointer to nothing, and should be used as the initial value for pointers because
using NULL will cause an error in most systems.
Example
char* cp; // declare pointer to char
char c, d; // declare char variables
A virtual function is a member function in base class that you expect to redefine in derived
classes.
Before going into detail, let's build an intuition on why virtual functions are needed in the first
place.
We created the Weapon class and derived two classes Bomb and Gun to load features of respective
weapons.
#include <iostream>
using namespace std;
class Weapon
{
public:
void loadFeatures()
{ cout << "Loading weapon features.\n"; }
};
int main()
{
Weapon *w = new Weapon;
Bomb *b = new Bomb;
Gun *g = new Gun;
w->loadFeatures();
b->loadFeatures();
g->loadFeatures();
return 0;
}
Output
We defined three pointer objects w, b and g of classes Weapon, Bomb and Gun respectively. And,
we called loadFeatures() member function of each objects using:
w->loadFeatures();
b->loadFeatures();
g->loadFeatures();
Works perfectly!
However, our game project started getting bigger and bigger. And, we decided to create a
separate Loader class to load weapon features.
This Loader class loads additional features of a weapon depending on which weapon is
selected.
class Loader
{
public:
void loadFeatures(Weapon *weapon)
{
weapon->features();
}
};
class Weapon
{
public:
Weapon() { cout << "Loading weapon features.\n"; }
void features()
{ cout << "Loading weapon features.\n"; }
};
class Loader
{
public:
void loadFeatures(Weapon *weapon)
{
weapon->features();
}
};
int main()
{
Loader *l = new Loader;
Weapon *w;
Bomb b;
Gun g;
w = &b;
l->loadFeatures(w);
w = &g;
l->loadFeatures(w);
return 0;
}
Output
Our implementation seemed correct. However, weapon features was loaded 4 times. Why?
Initially, the Weapon object w is pointing to the b object (of Bomb) class. And, we tried to load the
features of Bomb object by passing it to loadFeatures()function using l object to pointer (of
Loader class).
However, the loadFeatures() function of the Loader class takes pointer to object of
a Weapon class as an argument:
void loadFeatures(Weapon *weapon)
That's the reason weapon features are loaded 4 times. To solve this issue, we need to make
function of base class (Weapon class) virtual using virtual keyword.
class Weapon
{
public:
virtual void features()
{ cout << "Loading weapon features.\n"; }
};
class Weapon
{
public:
virtual void features()
{ cout << "Loading weapon features.\n"; }
};
class Loader
{
public:
void loadFeatures(Weapon *weapon)
{
weapon->features();
}
};
int main()
{
Loader *l = new Loader;
Weapon *w;
Bomb b;
Gun g;
w = &b;
l->loadFeatures(w);
w = &g;
l->loadFeatures(w);
return 0;
}
Output
Also, notice that, the l->loadFeatures(w) function calls the function of different classes
depending upon what l object is pointing.
Using virtual function made our code not only clearer but flexible too.
In the above program, weapon features is printed twice. We encourage you to add additional
code on the above program to load weapon features only once.
If we want to add another weapon (let's say knife), we can easily add and load features of
it. How?
Knife k;
w = &k;
l->loadFeatures(w);
It's worth noticing that we didn't change anything in the Loader class to load features of knife.
Sometimes, it's desirable to use inheritance just for the case of better visualization of the
problem.
In C++, you can create an abstract class that cannot be instantiated (you cannot create object of
that class). However, you can derive a class from it and instantiate object of the derived class.
class Weapon
public:
};
Here, the pure virtual function is
// Abstract class
class Shape
{
protected:
float l;
public:
void getData()
{
cin >> l;
}
// virtual Function
virtual float calculateArea() = 0;
};
int main()
{
Square s;
Circle c;
return 0;
}
Output
Area of square: 16
In this program, pure virtual function virtual float area() = 0; is defined inside
the Shape class.
One important thing to note is that, you should override the pure virtual function of the base
class in the derived class. If you fail the override it, the derived class will become an abstract
class as well.
C does not have built-in input/output facilities. Instead, it left the I/O to the compiler as external
library functions (such as printf and scanf) in stdio (standard input output) library. The ANSI C
standard formalized these IO functions into Standard IO package (stdio.h). Likewise, C++
continues this approach and formalizes IO in iostream and fstream libraries.
Streams in C++
C++ IO is based on streams, which are a sequence of bytes flowing in and out of the programs
(just like water and oil flowing through a pipe). I/O systems in C++ are designed to work with a
wide variety of devices including terminals, disks and tape drives. The input-output system
supplies an interface to the programmer that is independent of the actual device being
accessed. This interface is known as a stream. A stream is a sequence of bytes which acts
either as a source from which input data can be obtained or as a destination to which output
data can be sent. The source stream which provides data to the program is called the input
stream and the destination stream which receives output from the program is called the output
stream.
To perform input and output, a C++ program must follow the steps mentioned below:
void main()
int g;
cin>>g;
1. get(char *)
2. get(void)
Both of them can be used to fetch a character including a blank space, tab or new-line
character.
Code snippet
char ch;
cin.get(ch);
while(ch != '\n')
cout<<ch;
cin.get(ch);
Similarly, the function put(), a member of ostream class can be used to output a line of text
character by character.
Example:
cout.put ('g');
char ch;
cout.put(ch);
getline()
write()
The getline() function reads the entire line of texts that ends with a newline character. The
general form of getline() is:
The write() function displays the entire line of text and the general form of writing this function is:
endl Manipulator
endl is the line feed operator in C++. It acts as a stream manipulator whose purpose is to feed
the whole line and then point the cursor to the beginning of the next line. We can use \n (\n is an
escape sequence) instead of endl for the same purpose.
setw Manipulator
This manipulator sets the minimum field width on output.
Syntax:
setw(x)
Example:
/*
* File: main.cpp
* Author: Gautam
*/#include <iostream>
#include <iomanip>
int main() {
cout<<setw(10)<<"Basic"<<setw(10)<<basic<<endl
<<setw(10)<<"TA"<<setw(10)<<ta<<endl
<<setw(10)<<"DA"<<setw(10)<<da<<endl
<<setw(10)<<"GS"<<setw(10)<<gs<<endl;
return 0;
Program Output: