Experiment # 01 User Defined Data Types Structures & Classes Objective
Experiment # 01 User Defined Data Types Structures & Classes Objective
Experiment # 01 User Defined Data Types Structures & Classes Objective
Experiment # 01
User Defined Data Types
Structures & Classes
Objective:
Objective of this lab session is to understand and revise user defined data types i.e structures
and classes.
Software Tool: -
1. Microsoft Visual Studio
Theory:
Structures:
Many times, we have to store the elements of the different data types. Structure is composition of the
different variables of different data types, grouped under same name.
Example
struct {
char name[64];
char course[128];
int age;
int year;
} student;
Definitions of Structures
Each member declared in Structure is called member.
char name[64];
char course[128];
int age;
int year;
student
Structure Initialization
When we declare a structure then memory won’t be allocated for the structure. i.e only writing
below declaration statement will never allocate memory
struct student
{
int mark1;
int mark2;
1
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
int mark3;
};
struct student
{
char name[20];
int roll;
float marks;
}std1 = { "Pritesh",67,78.3 };
In the above code snippet, the structure is declared and as soon as after declaration we have
initialized the structure variable.
std1 = {"Pritesh",67,78.3};
struct student
{
char name[20];
int roll;
float marks;
}
std1 = {"Pritesh",67,78.3};
std2 = {"Don",62,71.3};
In this example, we have declared two structure variables in above code. After declaration of
variable we have initialized two variables.
std1 = {"Pritesh",67,78.3};
std2 = {"Don",62,71.3};
3: Initializing Single member
struct student
{
int mark1;
int mark2;
2
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
int mark3;
} sub1={67};
Though there are three members of structure, only one is initialized, and then remaining two
members are initialized with Zero. If there are variables of other data type then their initial
values will be
Data Type Default value if not initialized
Integer 0
Float 0.00
Char NULL
struct student
{
int mark1;
int mark2;
int mark3;
};
void main()
{
struct student s1 = {89,54,65};
- - - - --
};
struct student
{
int id;
char name[20];
float percentage;
3
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
};
void func(student);
int main()
{
student record;
record.id=1;
cin>>record.name;
record.percentage = 86.5;
func(record);
return 0;
}
int main()
{
student record;
record.id=1;
cout<<"enter student name: ";
cin>>record.name;
record.percentage = 86.5;
4
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
func(record);
struct student
{
int id;
char name[20];
float percentage;
};
struct student record; // Global declaration of structure
void structure_demo();
int main()
{
record.id=1;
cin>>record.name;
record.percentage = 86.5;
structure_demo();
return 0;
}
void structure_demo()
{
cout<<" Id is: "<< record.id;
cout<<" Name is: "<< record.name;
cout<<" Percentage is: "<< record.percentage;
}
5
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
Classes:
Classes are collections of data related to a single object type along with functions to access and
manipulate the data. The functions usually are the only way to modify and access the variables. This
might seem silly at first, but the idea to make programs more modular - the principle itself is called
"encapsulation". The key idea is that the outside world doesn't need to know exactly what data is stored
inside the class -- it just needs to know which functions it can use to access that data. This allows the
implementation to change more easily because nobody should have to rely on it except the class itself.
Description
The mechanism that allows you to combine data and the function in a single unit is called a class. Once a
class is defined, you can declare variables of that type. A class variable is called object or instance. In
other words, a class would be the data type, and an object would be the variable.
Inheritance
Inheritance is one of the key features of object-oriented programming including C++ which allows user
to create a new class (derived class) from an existing class (base class). The derived class inherits all
features from a base class and it can have additional features of its own.
6
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
Depending on Access modifier used while inheritance, the availability of class members of Super
class in the sub class changes. It can either be private, protected or public.
1) Public Inheritance
This is the most used inheritance mode. In this the protected member of super class becomes
protected members of sub class and public becomes public.
class Subclass : public Superclass
2) Private Inheritance
In private mode, the protected and public members of super class become private members of
derived class.
class Subclass : Superclass // By default its private inheritance
3) Protected Inheritance
In protected mode, the public and protected members of Super class becomes protected members
of Sub class.
class subclass : protected Superclass
Derived Class
7
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
Types of Inheritance
In C++, we have different types of Inheritance. Namely,
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance (also known as Virtual Inheritance)
Single Inheritance
In this type of inheritance one derived class inherits from only one base class. It is the most
simplest form of Inheritance.
Polymorphism
Polymorphism means having multiple forms of one thing. In inheritance, polymorphism is done,
by method overriding, when both super and sub class have member function with same
declaration but different definition.
Function Overriding
If we inherit a class into the derived class and provide a definition for one of the base class's
function again inside the derived class, then that function is said to be overridden, and this
mechanism is called Function Overriding
Requirements for Overriding
8
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
1. Inheritance should be there. Function overriding cannot be done within a class. For this
we require a derived class and a base class.
2. Function that is redefined must have exactly the same declaration in both base and
derived class, that means same name, same return type and same parameter list.
Virtual Functions
Virtual Function is a function in base class, which is override in the derived class, and which
tells the compiler to perform Late Binding on this function.
Virtual Keyword is used to make a member function of the base class Virtual.
Late Binding
In Late Binding function call is resolved at runtime. Hence, now compiler determines the type of
object at runtime, and then binds the function call. Late Binding is also called Dynamic Binding
or Runtime Binding.
Abstract Class
Abstract Class is a class which contains at least one Pure Virtual function in it. Abstract classes
are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must
provide definition to the pure virtual function, otherwise they will also become abstract class.
1. Abstract class cannot be instantiated, but pointers and references of Abstract class type can be
created.
2. Abstract class can have normal functions and variables along with a pure virtual function.
3. Abstract classes are mainly used for Upcasting, so that its derived classes can use its interface.
4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will
become Abstract too.
Pure virtual Functions are virtual functions with no definition. They start with virtual keyword
and ends with= 0. Here is the syntax for a pure virtual function,
virtual void f() = 0;
9
BSEE 3rd SemesterLAB 01Data Structure & Algorithms
1. Write a Parent Class Shape and define a virtual function display() in it . Derive classes
Rectangle and Triangle from Shape class. Override display() function in derived classes.
Make a pointer to base class. call the display function of rectangle and triangle using
pointer.
2. in the above program define height and width fields and write virtual functions
getArea(),setHeight(float) and setWidth(float) in shape class. Override getArea() in
Rectangle and Triangle to calculate the area . in Main() create objects of Rectangle and
Triangle and a Shape pointer to point them. Call the getArea() of both classes.
Lab Tasks:
1. We are developing a grading system for students which have the following information.
Student-Id, Student-Name, Department, Semester, Courses and Marks in Courses. A
course has following information (Course-ID, Course-Name, Marks).Define nested
structure for the above information.
a) Define a function that takes input from user to set student information.
b) Define a function that displays the output of student information.
2. Write a function called elapsed time with two parameters, the start time and the end time.
Each parameter is a structure with three fields showing the hours, minutes and seconds of
specified time. The function is to return a time structure containing the time elapsed
between the two parameters. You must handle the situation when the start time is in the
previous day.
3. Imagine the a publishing company, that markets both book and audiocassette versions of
its works. Create a class called publication that stores the title (a string) and price (type
float) of a publication. From this class derive two classes: book, which adds a page count
(type int); and tape, which adds a playing time in minutes (type float). Each of the three
classes should have a getdata() function to get its data from the user at the keyboard, and
a putdata() function to display the data. Write a main() program that creates an array of
pointers to publication. In a loop, ask the user for data about a particular book or tape,
and use new to create an object of type book or tape to hold the data. Put the pointer to
the object in the array. When the user has finished entering the data for all books and
tapes, display the resulting data for all the books and tapes entered, using a for loop.
10