Page 1 of 18

5

C + + Programming SECTION 1 C + + PROGRAMMING LAB

Structure Page Nos.

1.0 Introduction 5

1.1 Objectives 5

1.2 General Guidelines 6

1.3 Some C + + Compilers 6

1.4 Dev C + + IDE 7

1.5 Using Dev C + + IDE 10

1.6 Running Example Programs Using Dev C + + 15

1.7 Session Wise Problems 18

1.8 Summary 22

1.9 Further Reading 22

1.0 INTRODUCTION

Our attempt in this section is to make you familiar with object oriented programming

(OOP) using C + + . We will discuss about how to write and run C + + program. It is

essential for you to practice as much as possible for getting more and more

understanding in the programming and problem solving. So far you have learnt basics

of problem solving and data handling concepts in courses MCS-011 & MCS-023 and

their respective lab work. By now you must have experienced that by experimenting

with the tool/package/programming language, you gain valuable experience and your

efficiency has increased.

By now, you have obtained the practical skills of c programming languages. In C + +

, almost the same kind of conditional and logical statements are used as you have used

in c programming. To begin with you may consider the C + + programming as a

extention of c programming. To become a good C + + programmer, it is essential to

take one step at a time, while programming/ problem solving. Hence, during the

problem solving/ programming process, you need to have good understanding of

following:

 the tools you are using for program writing and running,

 basic language feature like data types, variables, loops and functions etc.,

 understanding of problem and deciding how to go from an idea to code, and

 a clear, understanding of OOP features need to be used in specific program.

In this lab course, we have used Dev-C + + , for writing and running the programs.

The Dev-C + + , is developed by Bloodshed Software. It is a full featured graphical

IDE (Integrated Development Environment), which is able to create Windows or

console-based C/C + + programs using the MinGW compiler system. We have also

added practice problems in this section, which you can attempt and enjoy C + +

programming.

1.1 OBJECTIVES

By the end of the practical sessions of this section, you will be able to:

 use basic constructs of C + + in your program;

Page 2 of 18

6

C + + Programming

Lab Course

 define and use objects in C + + programs;

 define and use constructors and destructor in C + + programs;

 use concept of inheritance and polymorphism in C + + programs;

 write programs for I/O handling in C + +;

 create your own C + + templets; and

 handle exceptions in C + +

programs.

1.2 GENERAL GUIDELINES

 You should come prepared for you lab session to properly utilize ther maximum

time of Lab session.

 You should attempt all lab exercises/assignments given in the list (session wise).

 You may seek assistance in doing the lab exercises from the available lab

instructor.

 For each program, you should add general comments such as your name date of

programming etc.

 There should be proper comments for description of the problem, requirement of

class, function etc. Proper comments are to be provided as and when necessary in

the programming.

 Your program should be interactive and properly documented with real Input/

Output data.

 Proper management of file of Lab record is necessary.Completed lab

assignments should be submitted in the form of a Lab Record in which you have

to write the algorithm, program code along with comments and output for various

inputs given.

 There are total 10 lab sessions ( of 3 hours each) for this lab course.

1.3 SOME C + + COMPILERS

For writing C + + program, you are free to use any C + + compiler and editor

available at your centre/home. If you get opportunity you may experiment with more

than one C + + compilers. In your c programming lab, you might have used Borland

C/C + + compiler. The use of Borland C/C + + compiler is explained to you in

BCSL-021 course. List of some compilers which are avauilable for free download is

given below. You are advised to check their conditions/licenses before using them.

 Apple C + + .

 Bloodshed Dev-C + + .

 Borland C + +

 Clang C + + ..

 Cygwin (GNU C + + )

 Digital Mars C + +

 DJ Delorie's C + + development system for DOS/Windows (GNU C + + )

 IBM C + + for IBM power, System Z, Bluegene, and Cell.

 Intel C + + for non-commercial development Sun Studio.

Page 3 of 18

7

Here you are re-introduced about working with Borland C/C + + compiler. For more C + + Programming

details you may refer to your BCSL-021 course.

Using Borland C/C + + Compiler

You may use the Borland c/ C + + compiler during the lab sessions under MS-DOS

connecting through Windows. A C + + program written in Borland C + + is saved

with .cpp extention.

To start Borland C/C + +

Click the Start button in the bottom left hand corner of the screen. The Start menu

pops up. Select Programs from the Start menu. Select Borland C/C + + from the

Programs menu. Select Borland C/C + + from the Borland C + + menu. In

summary the steps to launching Borland C/C + + are:

Start--->Programs--->Borland C + + --->Borland C + +

You should now proceed to the main window for the C/C + + development

environment.

Editing and Running C + + Program

You can create a program by entering text that corresponds to C + + statements into a

file.The compilatiuon end running process is same as explained in BCSL-021 course.

It is no note that Borland compilers for C/C + + runs on DOS prompt (Non-window).

Borland compiler for C/C + + is not having good interface compared to Dev C + +

IDE. The Dev C + + IDE run on windows and easy to use as it provides very good

GUI.

1.4 DEV C + + IDE

Dev-C + + , is a fully featured easy to use graphical IDE (Integrated Development

Environment), which is able to create Windows or console-based C/C + + programs

using the MinGW compiler system. MinGW (Minimalist GNU for Windows) uses

GCC (the GNU g++ compiler collection), which is essentially the same compiler

system that is in Cygwin (the unix environment program for Windows) and most

versions of Linux. The GNU General Public License (GPL) is a free, copyleft license

for software.

The Dev-C + + as available as free software (under GPL). Link to Bloodshed Dev-C

+ + sites is : http://www.bloodshed.net/devcpp.html.

You may download Dev-C + + and should installer put Dev-C + + in the default

directory of C:\Dev-Cpp. Hence, later on installation of add-ons or upgrades can be

easily done.

Dev C + + Installation:

To install Dev-C + + onto your computer , first download the free installation

program, copying it to a folder on youre computer.

1. Then install Dev-C + + onto yopur omputer by following the steps:

 Run the setup program.

 BE SURE to use the default directory for installation (C:\dev-cpp).