Welcome guest! Click here to register or login.
logo

<< Tutorial 14
Tutorial 16 >>

Other data types

***Please register FREE to rate this tutorial***


Topics
Structs
typedef
The arrow -> operator
Examples
Example 1
Example 2

Structs

Another feature of C++ similar to classes is the use of a struct. Here is the definition of a struct:

struct name{
	//data variables here.
};

Where in the above, the name is a useful name for the struct and the data variables are more than one variable with different data types. The struct is made for this just like a class. Notice that the semicolon after then closing brace is required.

Here is a nice example showing how to use a struct.

Example 1:
Struct program

Download source code here (Right click - Save Tagret As...)

#include <iostream>
#include <string>
using namespace std;

struct Person{

	string name;
	int age;
	char gender;

};

int main(){

	Person p;

	p.name = "Christopher";
	p.age = 34;
	p.gender = 'M';

	cout << "Name: " << p.name << endl;
	cout << "Age: " << p.age << endl;
	cout << "Gender: " << p.gender << endl;

	return 0;
}

The above program will output the contents of the person's struct. Notice that each of the variables are of different data types (int, string and char).

Typedef

In C++, the typedef keyword is used to allow another name for a data type. Here is how it is declared.

typedef existing_Type new_Name;

where in the above, existing_type is a primitive type such as int, float, char etcÂ… and new_Name is your useful name for it. Let's see an example:

#include <iostream>
using namespace std;

typedef char C;
typedef long int LI;

int main(){

	//same as saying: char c = 'D';
	C ch = 'D';

	//same as saying: long int I = 3;
	LI i = 3;

	//outputs 3D:
	cout << i << ch << endl;

	return 0;
}

This program simply shows how to use the typedef. Notice that the program could have been rewritten to the commented lines above.

Notice also that a typedef just creates another name for an already defined type, it does not redefine it. The principal around a typedef is to allow outside programmers to read and understand code better. This is a myth I might add.

The arrow -> operator

When dealing with pointers to a struct or a class, you can use the arrow operator -> to both dereference the pointer and assign a value at the same time. The variables must be declared public in a class for it to work.

Here is an example with a struct:

Example 2:
Sample arrow program

Download source code here (Right click - Save Tagret As...)

#include <iostream>
using namespace std;

struct Point{
	int x;
	int y;
};

int main(){

	Point* p = new Point;

	p->x = 9;
	p->y = 4;

	cout << p->x << " " << p->y << endl;

	return 0;
}

The output will be 9 4 when complete. Now here is an example with a class:

#include <iostream>
using namespace std;

class Point{

public:
	int x, y;

	Point(){
		x = 0; y = 0;
	}
};

int main(){

	Point* p = new Point;

	p->x = 9;
	p->y = 4;

	cout << p->x << " " << p->y << endl;

	return 0;
}

Notice again that the data variables are public.


<< Tutorial 14
Tutorial 16 >>