Professional Documents
Culture Documents
PL2 Lab1
PL2 Lab1
LAB MANUAL 01
CSC 2207 Programming Language 2 [EEE]
1
TITLE
Introduction to C++
PREREQUISITE
OBJECTIVE
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.
• 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.
• Code Blocks is open source, cross platform, free C, C++ and Fortran IDE.
• Demonstrate in the lab.
/*
Multiple line
comment
*/
//Single line comment
// include directive
#include<iostream>
// for cout and cin in the std namespace
using namespace std;
3
// displays Hello World! on screen
cout<<"Hello World!"<<endl;
#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 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;
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
6
C++ Arithmetic Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
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.
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
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.