C++ Programming

C++ Programming

User Defined Data Types


Introduction

Previously we have learnt that there are mainly two data types: Fundamental and Derived Data TypesThis module introduces another such constituent: User Defined Data Types. The data types that are defined by the user are referred to as user-defined data types.
These types include:

Let us briefly discuss each of these data types.

Class

C++ Class is a group of objects that share common properties and relationships. So then what do you mean by an object. An object is an identifiable entity with some characteristics and behaviour. A Class is a blueprint for the object.

We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. The house is the object. Since many houses can be made from the same description, we can create many objects from a class.

How to define a class in C++?

A class is defined in C++ using the “class” keyword followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end. Syntax:

class <class name>
{
//Data Members
//Member Functions
};

When class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects. Syntax to declare an object:

<class name> <object name>;

No need to break your head over, we’re gonna do in-depth analysis on classes in the further modules.





Structure

Structure is a collection of variables of different data types under a single name. It is similar to a class in that, but a structure cannot be inherited.

Let’s say you want to store some information about a person: his/her name, citizenship number and salary. You can easily create different variables like name, citno, salary to store these information separately.

However, in the future, you would want to store information about multiple persons. Now, you’d need to create different variables for each information per person: name1, citno1, salary1, name2, citno2, salary2 and so on.

You can easily visualize how big and messy the code would look. Also, since no relation between the variables would exist, it’s going to be a daunting task. A better approach will be to have a collection of all related information under a single name Person, and use it for every person. Now, the code looks much cleaner, readable and efficient as well.

This collection of all related information under a single name Person is a structure.

How to declare a structure in C++ programming?

The “struct” keyword defines a structure type followed by the name of the structure.                               

Example:

struct Person
{
char name[50];
int citno;
float salary;
};

How to define a structure variable?

When a structure is created, no memory is allocated. The structure definition is only the blueprint for the creating of variables. You can imagine it as a datatype. Now we need to define a structure variable:

Person jake;

Here, a structure variable jake is defined which is of type structure Person. When structure variable is defined, only then the required memory is allocated by the compiler. Similarly we can create multiple structure variables.

How to access members of a structure?

The members of structure variable is accessed using a dot (.) operator.

Suppose, you want to access the salary of structure variable jake and assign 5000 to it. You can perform this task by using following code below:

jake.salary= 5000;





Enumeration

An enumeration is a user-defined data type that consists of integral constants. It is used to assign names to the integral constants which makes a program easy to read and maintain. To define an enumeration, keyword enum is used.

enum season
{ spring, summer, autumn, winter };

Here, the name of the enumeration is season. And springsummer and winter are values of type seasonBy default, spring is 0summer is 1 and so on. You can change the default value of an enum element during declaration ( if required ).

enum season
{
spring = 7,
summer = 4,
autumn = 9,
winter = 24
};

When you create an enumerated type, only blueprint for the variable is created. Here’s how you can create variables of enum type.

<enumeration name> <variable>;

For the above program, let’s create a variable of enum type.

season today;





The Weekly Program

Now let’s create a program to find the area and circumference of a circle.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const float pi=3.14;
float area,circ,r;
cout<<"Enter the radius of the circle: ";
cin>>r;
area=pi*r*r;
circ=2*pi*r;
cout<<"Area of the circle: "<<area;
cout<<"Circumference of the circle: "<<circ;
getch();
}

Execution of the Program

The program is quite self-explanatory. Now Compile the program. If the compiler shows errors then check my program and resolve it. Then Run the program. Here’s the output:

Now it’s time to do it yourself and feel free to mess around with the program to understand in-depth what each statement does. Don’t forget to save your program.

That’s it for today, see you next week!!!