You are on page 1of 8

Content

- C++ Structures (Struct)


- C++ Enumeration (Enum)
- C++ Classes and Objects
-

Page 1 of 8

C++ Structures
A structure is a user-de ned data type that enables grouping variables of different data types under a
single name. It is similar to a  class  in that both holds a collection of data of different data types.
Structures can be referred to as a single variable and to its parts as  members  of that variable by
using the dot (.) operator. The power of structures lies in the fact that once de ned, the structure name
becomes a user-de ned data type and may be used the same way as other built-in data types, such as
int, double, and char.

De ning a Structure
A structure is de ned using the struct keyword followed by the name of the structure. Inside the
structure, you de ne variables of different data types that represent the members or elds of the
structure. Example
struct Student

string name;

string matric_no;

int age;

char gender;

In the above example, `Student` is the name of the structure, which can now be used as any other data
type. While name, matric_no, age and gender are the structure members.

Why Structures are Valuable


For instance, we want to process the bio-data of ve students containing their names, matric_no, age
and gender. Without structure, we will have to create 5 x 4 variables, e.g. name1, matric_no1, age1
and gender1 but with structure, we will only need ve variables, e.g. s1, s2, s3, s4 & s5.

Declaring Variables of Type Struct


To utilise the structure type, we have to create variables that are of the user-de ned structure type. For
example, in the program below, we will create two variables of the `Student` type.
int main()

Student s1, s2;

return 0;

In the above example, we create two variables, s1 and s2, that are of the Student type. That is how to
declare variables of the type Struct. Next, we will look at how to access the member variables.

Page 2 of 8

fi
.
.
.

fi
fi

fi

fi

fi
fi

fi
fi

fi

Accessing Structure Members


Structure members are accessed using the dot (.) operator. To access a member, you specify the structure
variable followed by a dot (.) and then the member name. For example, to access the name of s1, we
will do the following: s1.name

Initialising Structure Variables


Structures can be initialised at the time of declaration using an initialiser list enclosed in curly braces
{}. The values are assigned to the structure members in the order they are de ned. So, let’s declare and
initialise s1.
Student s1 = {“Okechukwu Christian”, “2021/SC/0419”, 21, ‘M’}

Nested Structures
Structures can be nested inside other structures, allowing you to create complex data structures. Nested
structures are accessed using the dot (.) operator with multiple levels of nesting. For example,
struct Point

int x;

int y;

struct Triangle

Point p1;

Point p2;

Point p3;

int main()

Triangle t;

t.p1.x = 5;

t.p1.y = 7;

return 0;

}
Exercise:
1. De ne a dob (Date of Birth) structure that can store the year, month and day of birth.
2. De ne a student structure that can store the rst name, last name, matric number, gender and date
of birth of a student. For the date of birth, use the structure you de ned in number 1.

Page 3 of 8

fi
fi
.
.
.

fi
fi

fi

C++ ENUMERATION (Enums)


An enum (enumeration) is a user-de ned data type that allows you to de ne a set of named values. It
provides a way to associate meaningful names with a set of related constants.

De ning an Enum
An enum is declared using the enum keyword followed by the name of the enum. Inside the enum, you
de ne a list of named constants, called enumerators, separated by commas. The enumerators are
assigned default integer values starting from 0 and incrementing by 1.
For example, we can de ne an enumeration for grade points as follows:
enum GradePoint { F, E, D, C, B, A };

Automatically, F is assigned the integer value 0, E = 1, D = 2, C = 3, B = 4 and A = 5.

Assigning Speci c Values


You can assign speci c integer values to the enumerators by providing an initialiser. The subsequent
enumerators without an initialiser are assigned values one greater than the previous enumerator.
enum Day {

SUNDAY = 1,

MONDAY = 2,

TUESDAY, 3

WEDNESDAY 4

};

Or

enum GradePoint {A = 5, B = 4, C = 3, D = 2, E = 1, F = 0}

Accessing Enum Values


Enum values are accessed using the enum name followed by the enumerator name. They can be used as
constants in your program.

Exercise
1. De ne a season enum that enumerates the four prominent seasons in the world in the order they
occur.

Page 4 of 8

fi
fi
fi

/
/
fi

/
/

fi

fi

fi

fi

C++ CLASSES AND OBJECTS


In previous classes, we learned about functions and variables. Sometimes it is desirable to put related
functions and data in one place so that it is logical and easier to work with. For example, we need to
store the length, breadth, and height of a rectangular room and calculate its area and volume. To
handle this task, we can create three variables, say,  length,  breadth, and  height  along with the
functions calculateArea() and calculateVolume().

However, in C++, rather than creating separate variables and functions, we can also wrap these
related data and functions in a single place (by creating  objects). This programming paradigm is
known as object-oriented programming. But before we can create  and use objects in C++, we rst
need to learn about classes.

C++ Class
A class is a user-de ned data type that serves as a blueprint for creating objects. It encapsulates data
(member variables) and functions (member functions) that operate on that data. Classes provide a way
to model real-world entities and de ne their behaviour and properties. We can think of a class as a
sketch (prototype) of a house. It contains all the details about the oors, doors, windows, etc. Based on
these descriptions, we can build the house. House is the object.

Create a Class
A class is de ned in C++ using the keyword class followed by the name of the class. The body of the
class is de ned inside the curly brackets and terminated by a semicolon at the end.
class className {

Some data (properties)

Some functions (methods)

};
Example:
class Room {
public:
double length;
double breadth;
double height;

double calculateArea(){
return length * breadth;
}

double calculateVolume(){
return length * breadth * height;
}
};
Page 5 of 8

.
.
.
.
.
.
/
/

/
/
fi

fi

fi

fi

fl

fi
In the above example, we de ned a class named Room. The variables  length,  breadth,

and  height  declared inside the class are known as  data members or properties. And the
functions calculateArea() and calculateVolume() are known as member functions or methods of a class.

C++ Objects
When a class is de ned, only the speci cation for the object is de ned; no memory or storage is
allocated. To use the data and access functions de ned in the class, we need to create objects.

Syntax to De ne Object in C++


className objectVariableName;

We can create objects of Room class (de ned in the above example) as follows:
int main(){

Create objects

Room room1, room2;

Here, two objects  room1  and  room2  of the  Room  class, are created in  the main function. We can
create objects of a class in any function of the program. We can also create objects of a class within
the class itself or in other classes. Also, we can create as many objects as we want from a single class,
just like you can build many houses with the same blueprint.

C++ Access Data Members and Member Functions


We can access the data members and member functions of a class by using the . (dot) operator. For
example, room1.calculateVolume();
This will call the calculateVolume() function inside the Room class for object room1.
Similarly, the data members can be accessed as room1.length = 5.5;

In this case, it initialises the length variable of room1 to 5.5.

In this example program below, we have used the  Room  class and its object  room1  to calculate the
area and volume of a room. In  the main() function, we assigned the values of  length,  breadth,
and height with the code:
room1.length = 79.2;

room1.breadth = 44.1;

room1.height = 21.7;

We then called the functions  calculateArea()  and  calculateVolume()  to perform the necessary
calculations. Note the use of the keyword public in the program. This means the members are public
and can be accessed anywhere from the program.

Page 6 of 8

/
/

fi
fi

fi

fi
fi

fi

fi

Example 1: Object and Class in C++ Programming


Program to illustrate the working of
objects and class in C Programming

#include <iostream>
using namespace std;

create a class
class Room {

public:
double length;
double breadth;
double height;

double calculateArea() {
return length * breadth;
}

double calculateVolume() {
return length * breadth * height;
}
};

int main() {

create object of Room class


Room room1;

assign values to data members


room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

calculate and display the area and volume of the room


cout "Area of Room = " room1.calculateArea() endl;
cout "Volume of Room = " room1.calculateVolume() endl;

return 0;
}

Page 7 of 8
/
/
/
/
/
/

/
/
/

/
/
/
<
<

<
<

<

<
<

<

<
<

<
<

Access Speci ers


Access speci ers (public, private, protected) control the visibility and accessibility of class
members. By default, members are private if no access speci er is speci ed. public members are
accessible from anywhere, while private members are only accessible within the class.

As per our needs, we can also create private members using the private keyword. The private members
of a class can only be accessed from within the class. For example,
 
class Test {

private:

int a;

void function1() { }

public:

int b;

void function2() { }

Here, a and function1() are private. Thus they cannot be accessed from outside the class. On the other
hand, b and function2() are accessible from everywhere in the program.

Page 8 of 8

fi
fi

fi

fi

You might also like