06 - CSC 201 - Struct, Enum & OOP
06 - CSC 201 - Struct, Enum & OOP
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.
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
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
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 };
SUNDAY = 1,
MONDAY = 2,
TUESDAY, 3
WEDNESDAY 4
};
Or
enum GradePoint {A = 5, B = 4, C = 3, D = 2, E = 1, F = 0}
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
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 {
};
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.
We can create objects of Room class (de ned in the above example) as follows:
int main(){
Create objects
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.
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
#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() {
return 0;
}
Page 7 of 8
/
/
/
/
/
/
/
/
/
/
/
/
<
<
<
<
<
<
<
<
<
<
<
<
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