0% found this document useful (0 votes)
42 views

Itp Lab 13-14

The document discusses C++ structs and classes, which allow programmers to create custom data types that group together related data. It provides examples of defining a BookRec struct to store book data like title, author, and cost. A StudentRec struct is also defined. Accessing struct fields uses the dot operator, like variable.field. Nested structs can be used as fields within other structs. Exercises demonstrate defining structs, accessing fields, and modifying nested struct data.

Uploaded by

Sahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Itp Lab 13-14

The document discusses C++ structs and classes, which allow programmers to create custom data types that group together related data. It provides examples of defining a BookRec struct to store book data like title, author, and cost. A StudentRec struct is also defined. Accessing struct fields uses the dot operator, like variable.field. Nested structs can be used as fields within other structs. Exercises demonstrate defining structs, accessing fields, and modifying nested struct data.

Uploaded by

Sahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DEPARTMENT OF TELECOMMUNICATION ENGINEERING

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO


INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________

Name: _____________________________________________ Roll No: _____________

Score: ____________Signature of the Lab Tutor: _____________ Date: _____


________________________________________________________________________

To Introduce Usage of Structures & Classes in C++

PERFORMANCE OBJECTIVE
Upon successful completion of this lab, the student will be able to:
becoming familiar with file & class in C++ and its usage in developing program modules.
C++ Data Types
Type statements in C++ are used to allow the compiler to:

• reserve blocks of memory to store information and


• give the reserved blocks of memory a symbolic name so that the data contained in this block of
memory can be manipulated by referring to this name in future C++ statements.

C++ has three basic data types available for the programmer: simple data types, structured data types and
address types. We have previously studied the simple data types such as int, float, char, and
string. Selected structured data types will be discussed in this lab (specifically, the C++ struct and the
C++ class). Address types will be reserved for more advanced C++ study.

Simple data types include the int type, the float type and the char type, for example. These types differ
mainly in "how" the data is stored. For example, the char type will use 8 bits (1 byte) of memory while
the int type will use 16 or more bits (2 bytes or more) of memory. Simple data types have a major
restriction -- variables which are declared as a simple data type may only store one data item. For
example, if the following declaration is made:

int oneNumber;

oneNumber may only store one integer value. There are many applications where it would be beneficial to
have one variable that could store several different types of information. Many programs are built around
a collection of different values that are all related in some way. For example, consider a collection of
information related to a book. A book has a title; it has an author, a publisher's name, and a retail cost of
the book. In database terminology, collections of related information are called records and the individual
data items contained in the record are called fields. If we wished to work with a book record in C++, for
example, we might list the different fields of the record. Also, we would need to determine the data type
for each field. Lastly, we should determine a name for each of the fields. The following table briefly
describes information that might be included in a book record.

Field Data Field


Field
Type Name
title of book string title
Author of
string author
book
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________
Publisher string publisher
retail cost of
float cost
book

Exercise 1:
Suppose that we want to store information about a student in a record. This will require several fields. On
the answer sheet, complete the following table with your ideas regarding the types and names of fields
that could be included in a student's record.
Field Field Data Type Field Name
a student's
name
a student's
tuition
a student's gpa
a student's major

Programmers often have the need to work with records and C++ provides mechanisms for accomplishing
this with the use of the struct structured data type. Let us now consider the C++ struct.

The C++ Struct


A C++ struct provides a mechanism for programmers to create a group of related items, called members,
that are not necessarily of the same types. The C++ struct allows the programmer to create a new type
which can store several pieces of information. A C++ struct may store data and functions. Therefore, we
say that a struct contains two types of members, data members (data) and function members (methods).
Usually, structs contain only data members and very few, if any methods. We will discuss methods more
completely later in this lab.

There are some basic beginning questions that need to be addressed related to using records (structs) in
C++.

1) What is the syntax for setting up a struct data type?


2) How do we access the fields contained in a struct?
3) Can structs be used as field components of another struct?
1) What is the syntax for setting up a struct data type?

The basic syntax is:

struct NameType
{

MemberList; //listing of variable names and their associated types


};
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________
For example, suppose that we wish to set up a C++ struct to store book information as described above.
The actual declaration might appear as follows:

struct BookRec

{
string title;
string author;
string publisher;
float cost;
};

Now, we have a new C++ type; it is called a BookRec. WARNING: Please remember that BookRec is
a type, not a variable. If we wish to use this new variable type, we must set up a variable using
theBookRec type. For example, the following statement creates a variable called myBook.

BookRec myBook;

Exercise 2:
On the answer sheet, show C++ statements to set up a new type called StudentRec to store information
about a student. This answer should relate to your answer to Exercise 1. Also, show C++ to set up a
variable called aStudent of type StudentRec.

2) How do we access the fields contained in a struct?

The ".", called the dot operator, is used to access members of a struct. Consider the variable myBook.
If myBook is a struct variable and title is a member of that struct, then myBook.title would be used to
access the title of the book. If we wished to read in all infomation for the myBook variable, we could use
the following statements.

cout<< "Enter book title: ";

getline(cin, myBook.title);

cout << "Enter book author: ";


getline(cin, myBook.author);
cout << "Enter publisher: ";
getline(cin,myBook.publisher);
cout << "Enter cost of book: ";
cin >> myBook.cost;

Note in the statement, getline(cin, myBook.title); in the example above, that myBook is a variable which
refers to a data type struct BookRec and myBook.title refers to the title portion of the struct BookRec. The
statement getline(cin, myBook.title); allows us to read in the title of a book object called myBook.
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________
Exercise 3:
On the answer sheet, show one C++ statement to perform each of the following for the
variable, aStudent, as described in Exercise 2.

a) Read in a student's name


b) Add $200.00 to the student's tuition.
c) If the student's major is "Computer Science", print out the student's name and his/her gpa.

3) Can structs be used as field components of another struct?

Yes! Suppose that we wish to store additional information in our book record related to the date of
publication. In this case, we may want to set up an additional record to store date information. For
example, note the following date record type:

struct DateRec
{

int year;
int month;
int day;

};

Now, we can add an additional field to our book record as shown below:

struct BookRec
{

string title;
string author;
string publisher;
float cost;
DateRec publishDate;

};

To gain access to the "nested" struct, we will need to use the dot operator twice (the first period is used
to access the publishDate field of the BookRec and the second dot operator is used to access the fields of
the publishDate record. The following statement allows us to print out the publication date for
the myBook variable.

cout << myBook.publishDate.month

<< myBook.publishDate.year

<< myBook.publishDate.day

<< endl;
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________

Exercise 4:
On the answer sheet, show code to change the year of myBook’s publication date to 1999. (HINT: Use an
assignment statement.)

Exercise 5:
Copy the program called cla11a.cc to your account. This program uses BookRec as described above.
Using this as an example, modify the code to:

• Set up a type called StudentRec and a variable called aStudent.


• Read in all student information. (Use your name for the student's name!)
• Print out all student information.

Compile and execute the program. Turn in a script containing a cat of your edited version of cla11a.cc file
and a run of this program.

The C++ Class


C++ has an additional structured data type called the class. It is very similar to the C++ struct. The main
difference being that the struct data type usually contains only a collection data members while
theclass data type usually contains a collection of data members and function members (methods) that
manipulate the data contained in the class.

The C++ class provides a mechanism for programmers to create objects. A software object is a self-
contained computing entity which has its own data and its own associated operations called methods. We
say that the C++ class supports data encapsulation (combining data and functions on that data into a
single program unit). In real life, we deal with physical objects continually. When we sit in a chair (an
object) at our desk (an object), we might consult our calendar (an object) to plan our activities for the day.
Think of an object that we experience and use daily, say an automobile. The automobile object has data
or data features (a steering wheel, an ignition, is either straight shift or automatic, etc.) and it has certain
functions or operations which it should perform. Some of the functions include: move forward, move
backward, start motor, stop motor, turn lights on, etc.

Objects have a public interface and a private component. The public interface of an object is composed of
the collection of functions and features which are available to the user to allow effective use of the
object. For example, the public interface of the “automobile” object would include buttons on the
dashboard to allow the user to control certain operations like turning on the lights and allowing the user
to use a key to start the motor. Private data or functions are unavailable to the public. This would be like
disabling the hood release so that the user cannot access the internal parts of an automobile (which
would be a good idea in some cases). Normally, private data and functions are those data and instructions
that should not be changed by the user.

Suppose that we wish to create a book object. We have previously considered what the data might look
like for this object (book title, book author, publisher, etc). The following table indicates possible
functions that we might want to include in this object.

Function Name Function Purpose


PrintBook Prints out the data contained in a book
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________
object
ChangeCost Changes the cost of a book
IsAvailable Determines whether the book is
currently available

A large portion of a programmer's job is to design objects and then use the C++ class mechanism to
actually define the object and determine how the object is to be manipulated. To design the object, the
programmer first thinks of the type of data that is needed to form the object. Secondly, the programmer
determines what functions are needed to manipulate the object. The C++ class allows a programmer to
create this new type. There are many benefits for creating objects using the C++ class. Some of these
benefits include data encapsulation, data security and information hiding. Information hiding refers to the
process of hiding implementation details from the users (called clients) of the created class or object. The
C++ class can be set up to “hide” implementation details from the user and can restrict the user from
seeing how the data is actually stored. This promotes data security since the user cannot “see” the data
contained in the object and thus, cannot manipulate the data erroneously. The C++ class promotes
"encapsulation" - the concept of binding the data and functions on the data items into one single
program unit.

How do we set up a C++ class?

When creating a class, the programmer should (usually) construct the class in two parts: a specification
file and an implementation file. The specification file declares the object ( indicates the data types and
functions of the class).The implementation file contains the definitions of the class methods (member
functions).We will examine the specification file first.

The C++ class specification file

Suppose that we wish to design a rectangle object. We must decide 1) what data should be included in
this object and 2) what functions are needed for this object. The data needed for this object should
include a rectangle's dimensions: width and height of the rectangle. Some of the functions needed might
include: a function to print a rectangle, a function to compute the area of the rectangle, a function to
compute and print the perimeter of a rectangle, a function to draw a rectangle and a function to allow the
user to set the length and width of the rectangle. Now we are ready to construct the class. The basic
syntax is:

class name

public:

//declare all functions and variables that are available


//to the user of the class

private:

//declare all functions and variables that should not be


//available to the user
};
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________
For example, the declaration of the rectangle class may appear as follows:

//FILE:cla11rectangle.h
class RectangleType
{
public:
void PrintRectangle();
float ReturnArea();
float ReturnPerimeter();
void SetSize(float L, float H);

private:
float length;
float height;
};

The RectangleType class has six members - four member functions (PrintRectangle, ReturnArea,
ReturnPerimeter and SetSize) and two member variables (length and height). The member variables form
the data that is needed to create a rectangle and the member functions allow the user to perform
different operations with a rectangle. Note that in this example, clients (users) of this class have access to
the member functions but do not have access to the member variables.

Exercise 6:
Design a class called CircleType and then create (using nedit) the class specification file
called, cla11circle.h, which contains functions and data members similar to the functions and data
members contained in the RectangleType class. NOTE: Data members are generally, but not always,
placed in the private portion of the class. What data members will be needed for a circle?(radius)

The C++ Implementation File


Next, the programmer should build the implementation file. The implementation file for the rectangle
object should contain the function definitions for all functions that were declared in the specification file,
cla11rectangle.h. IMPORTANT: Note the following:

1. Functions in the implementation file have access to the private data members of the class!
2. The scope resolution operation, "::", is always used in the function header. This informs the
compiler to what class the member function belongs.

Observe the following implementation file that might be used to define the functions contained in
cla11rectangle.h.

//FILE: cla11rectangle.cc

#include "cla11rectangle.h"
#include <iostream>
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________

using namespace std;

//Sets the length and height of the rectangle


void RectangleType::SetSize(float l, float h)
{
length = l;
height = h;
}

//Prints the length and height of the rectangle


void RectangleType::PrintRectangle()
{
cout << "RECTANGLE\n";
cout << "Rectangle Height:" << height << endl;
cout << "Rectangle Length:" << length << endl;
}

// Computes and returns the area of the rectangle


float RectangleType::ReturnArea()
{
return (length * height);
}

//Computes and returns the perimeter of the rectangle


float RectangleType::ReturnPerimeter()
{
return (2*length + 2* height);
}

Exercise 7:
Write the implementation file for the circle class and save it using the name cla11circle.cc. Note:The area
of a circle is pi*r*r where pi=3.1415 and r is the radius of a circle. The perimeter (circumference) of a
circle is found using the formula, 2 * pi * r.

Now, how do we use the rectangle class that we have just created? Consider the following example of a
client file (a file that declares and manipulates objects of a particular class).

//FILE: cla11b.cc

#include "cla11rectangle.h" //#include file created by the programmer


#include <iostream>

using namespace std;


DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________
int main()
{
//declare variables
RectangleType aRectangle, bRectangle; //Set up two rectangle objects

//Set length and width of rectangle


aRectangle.SetSize(10, 5);

//Print the dimensions of a rectangle


aRectangle.PrintRectangle();

//Print statistics for the aRectangle


cout << "Rectangle Area: ";
cout << aRectangle.ReturnArea() << endl;
cout << "Rectangle Perimeter: ";
cout << aRectangle.ReturnPerimeter() << endl;
cout << endl << endl;

//Using assignment statement with objects


bRectangle=aRectangle;

//Print statistics for bRectangle


bRectangle.PrintRectangle();

return 0;
}

Note that the dot operator is used to invoke member functions.This is how “messages” are passed to an
object in C++. Thus,
aRectangle.SetSize(5, 10);
sends the SetSize message to the object called aRectangle. Note the assignment statement after the
cout statements. This line is valid and copies all data fields from the object, aRectangle, to the
object,bRectangle because we have simple data types making up the two objects.

Note that nowhere in our main function do we try to access the data variables, length and height, which
were defined in the RectangleType class. In fact, we could not do so because these data members were
placed in the private section of the class. This means that only members of the class can access them.

Compiling and Linking with Multiple Files

Exercise 8:
The above example is a very simple example of a main program that utilizes the rectangle class. All of the
rectangle class files have been created for you (cla11rectangle.h, cla11rectangle.cc, cla11b.cc). Copy these
files to your account. Both the main function and the implementation functions for the rectangle class
must be compiled. Since they are in separate files, a slight variation of the aCC statement is required. To
compile and link this program, type
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 13-14
ST

________________________________________________________________________

aCC clal1rectangle.cc cla11b.cc -o rectangle

Compile and run this program to verify that the program runs properly - Do not submit this run for
grading.

Exercise 9:
You have created two additional files, cla11circle.h and cla11circle.cc.Edit the cla11b.cc file so that it
performs the following activities. IMPORTANT: You must tell the compiler to include your cla11circle.h file
---Use: #include "cla11circle.h"

1. Create a rectangle object with a width and height that is input by the user and then call all of the
functions in the rectangle class.
2. Create a circle object with a radius that is input by the user and then call all of the functions in
the circle class.

Exercise 10:
Compile your program and turn in a script containing a cat of the files (cla11circle.h, cla11circle.cc,
cla11b.cc), a compile and a run of this program. IMPORTANT: To compile, you might want to use:

aCC cla11rectangle.cc cla11circle.cc cla11b.cc -o circlerectangle

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy