Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

American International University Bangladesh (AIUB)

Faculty of science & Technology


Department of Computer Science

LAB MANUAL 01
CSC 2207 Programming Language 2 [EEE]

1
TITLE

Introduction to C++

PREREQUISITE

• To be able to write, build and run a C program in CodeBlocks.


• To be able to identify and understand the basic components of a C program
• To be able to use the library functions cout and cin
• To have basic idea about variable and type casting.

OBJECTIVE

• To know about variable and data types


• To know about different Operators
• To be able to solve the exercises, lab work and assignments at the end of this manual

THEORY

History of C++

The C++ language is an object-oriented programming language & is a combination of both low-
level & high-level language – a Middle-Level Language. The programming language was
created, designed & developed by a Danish Computer Scientist – Bjarne Stroustrup at Bell
Telephone Laboratories (now known as Nokia Bell Labs) in Murray Hill, New Jersey. C++ was
initially known as “C with classes, ” and was renamed C++ in 1983.

Procedural vs. OOP

• In procedural programming, program is divided into small parts called functions whereas
in object oriented programming, program is divided into small parts called objects.
• There is no access specifier in procedural programming whereas Object oriented
programming have access specifiers like private, public, protected.
• Procedural programming does not have any proper way for hiding data so it is less secure
whereas Object oriented programming provides data hiding so it is more secure.

2
• In procedural programming, function is more important than data. In object oriented
programming, data is more important than function. Procedural Examples: C,
FORTRAN, Pascal, Basic etc. OOP Examples: C++, Java, Python, C# etc.

Object Oriented Programming

Object-oriented programming (OOP) is a programming paradigm based on the concept of


"objects", which can contain data, in the form of fields (often known as attributes or properties),
and code, in the form of procedures (often known as methods). There are four Pillars of Object
Oriented Programming:

I. Abstraction: Abstraction of Data or Hiding of Information is called Abstraction!


II. Encapsulation: Binding of Data and Functions together and keep both safe from outside
interference and misuse is called Encapsulation.
III. Inheritance: Inheritance enables new objects to take on the properties of existing objects.
IV. Polymorphism: Polymorphism is derived from 2 Greek words: poly and morphs. The
word "poly" means many and "morphs" means forms. So polymorphism means "many
forms".

Coding environment: Code Blocks IDE

• Code Blocks is open source, cross platform, free C, C++ and Fortran IDE.
• Demonstrate in the lab.

Basic building blocks of C++ program

/*
Multiple line
comment
*/
//Single line comment

// include directive
#include<iostream>
// for cout and cin in the std namespace
using namespace std;

//This is where the execution of program begins


int main()
{

3
// displays Hello World! on screen
cout<<"Hello World!"<<endl;

// declare integer variable and take input from user.


int a;
cin>> a;
//print variable a
cout<<a<<endl;
return 0;
}

#include<iostream>
using namespace std;

int main()
{
double a, b, c;
cout<<"Enter two number: ";
cin>>a>>b;
c = a+b;
cout<<"the result: "<<c;
return 0;
}

Keywords and Identifiers in C++

Keywords are predefined words that have special meanings to the compiler. This is also called
Reserved Words. For example, int money; Here, int is a keyword that indicates money is a
variable of type integer. List of C++ Keywords.

Library Identifiers

These words are supplied default meanings by the programming environment, and should only
have their meanings changed if the programmer has strong reasons for doing so. Examples are
cin, cout and sqrt (square root) from C++ math Header().

Programmer-supplied Identifiers

4
These words are "created" by the programmer. Identifiers are the unique names given to
variables, classes, functions, or other entities by the programmer. For example, int money;
double accountBalance;

Rules for naming identifiers

• Identifiers can be composed of letters, digits, and the underscore character.


• It has no limit on name length.
• It must begin with either a letter or an underscore.
• It is case-sensitive.
• We cannot use keywords as identifiers.

Data Type or variable:

C++ Fundamental Data Types:

int ------ Integer ------ 2 or 4 bytes

float ------ Floating point ------ 4 bytes

double ------ Double Floating-point ------ 8 bytes

char ------ Character ------ 1 byte

bool ------ Boolean ------ 1 byte

5
C++ Type Modifiers

We can further modify some of the fundamental data types by using type modifiers. There are 4
type modifiers in C++. They are: signed unsigned short long.

#include<iostream>
using namespace std;

int main()
{
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number

double myDoubleNum = 9.98; // Floating point number


char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
return 0;
}

6
C++ Arithmetic Operators

Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)

C++ Relational Operators

Operator Meaning Example

== Is Equal To 3 == 5 gives us false

!= Not Equal To 3 != 5 gives us true

> Greater Than 3 > 5 gives us false

< Less Than 3 < 5 gives us true

>= Greater Than or Equal To 3 >= 5 give us false

<= Less Than or Equal To 3 <= 5 gives us true

More details:

1. http://www.cplusplus.com/doc/tutorial/operators/
2. https://www.programiz.com/cpp-programming/operators

LAB WORK

a. Write a program that prints your Name, ID and Department in three separate lines.

b. Write a program to take input your Name, ID and Department and print them.

c. Write a program that creates an integer variable assigns the value 90 to it and then prints.
7
d. Write a program that creates two double variables assigns values to them and print.

e. Write a program that takes one integer variable as input from the user and checks if it is even.

f. Write a program that takes length and breadth as input from the user and prints the area of a
rectangle as output.

g. write a program to find area of circle. Hints: Area = PI x radius x radius

ASSIGNMENT

a. There are three resistors in a circuit. Resistor 1 has value 4 ohm. Resistor2 has value 8 ohm
and Resistor 3 has value 1 ohm. Write a program that gives the following output

Value of resistor 1: 4 ohm

Value of resistor 2: 8 ohm

Value of resistor 3: 1 ohm

b. Write a program that takes two inputs from the user and adds, subtracts, multiplies and divide
it and also print the output.

c. Write a program that converts 1 degree Celsius into its equivalent Kelvin value.

You might also like