Lab N O. 8: Introduction To Fundamentals of C++ Programing Pre Lab Task
Lab N O. 8: Introduction To Fundamentals of C++ Programing Pre Lab Task
Objectives:
Theory:
Programming language
A Programming language is a formal constructed language designed to communicate
instructions to a machine, particularly a computer. Programming languages can be used to
create programs to control the behavior of a machine or to express algorithms.. Traits often
considered important for what constitutes a programming language include:
Abstractions
Programming languages usually contain abstractions for defining and manipulating data
structures or controlling the flow of execution. The practical necessity that a programming
language support adequate abstractions is expressed by the abstraction principle; this
principle is sometimes formulated as recommendation to the programmer to make proper use
of such abstractions.
Write Types of programming languages.
Object-oriented languages
Object-oriented languages help to manage complexity in large programs. Objects package
data and the operations on them so that only the operations are publicly accessible and
internal details of the data structures are hidden.
C++
The C++ language, developed by Bjarne Stroustrup at AT&T in the mid-1980s, extended C
by adding objects to it while preserving the efficiency of C programs. It has been one of the
most important languages for both education and industrial programming. Large parts of
many operating systems, such as the Microsoft Corporation’s Windows 98, were written in
C++++.
Fig 1
1. Declaring Structure
2. Declaring Class
3. Declaring Variable
Actually this section can be considered as sub section for the global declaration section.
Class declaration and all methods of that class are defined here.
Section 4: Main Function
Each and every C++ program always starts with main function.
This is entry point for all the function. Each and every method is called indirectly through
main.
Object - Objects have states and behaviors. Example: A dog has states - color, name, breed
as well as behaviors - wagging, barking, eating. An object is an instance of a class.
Class - A class can be defined as a template/blueprint that describes the behaviors/states that
object of its type support.
Instance Variables - Each object has its unique set of instance variables. An object's state is
created by the values assigned to these instance variables.
#include <iostream>
#include<conio.h>
int main()
cout << "Hello World"; // prints Hello World
getch ( );}
1. Let's look at how to save the file, compile and run the program. Please follow the
steps given below:
2. Open a text editor and add the code as above.
3. Save the file as: hello.cpp
4. Open a command prompt and go to the directory where you saved the file.
5. Type 'g++ hello.cpp ' and press enter to compile your code. If there are no errors in
your code the command prompt will take you to the next line and would generate
a.out executable file.
6. Now, type ' a.out' to run your program.
7. You will be able to see ' Hello World ' printed on the window.
8. $ g++ hello.cpp
9. $ ./a.out
10. Hello World
11. Make sure that g++ is in your path and that you are running it in the directory
containing file hello.cpp.
12. You can compile C/C++ programs using makefile. For more details, you can
check Makefile Tutorial.
Semicolons & Blocks in C++:
In C++, the semicolon is a statement terminator. That is, each individual statement must be
ended with a semicolon. It indicates the end of one logical entity.
x = y;
y = y+1;
add(x, y);
A block is a set of logically connected statements that are surrounded by opening and closing
braces. For example:
return 0;
C++ does not recognize the end of the line as a terminator. For this reason, it does not matter
where on a line you put a statement. For example:
x = y;
y = y+1;
add(x, y);
is the same as
Key words:
Bool explicit Private true
C++ Identifiers:
A C++ identifier is a name used to identify a variable, function, class, module, or any
other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore
(_) followed by zero or more letters, underscores, and digits (0 to 9).
C++ does not allow punctuation characters such as @, $, and % within identifiers. C++ is a
case-sensitive programming language. Thus, Manpower and manpower are two different
identifiers in C++.
C++ Keywords:
The following list shows the reserved words in C++. These reserved words may not be used
as constant or variable or any other identifier names.
Trigraphs:
A few characters have an alternative representation, called a trigraph sequence. A
trigraph is a three-character sequence that represents a single character and the sequence
always starts with two question marks.
Trigraphs are expanded anywhere they appear, including within string literals and character
literals, in comments, and in preprocessor directives.
Trigraph Replacement
??= #
??/ \
??' ^
??( [
??) ]
??! |
??< {
??> }
??- ~
All the compilers do not support trigraphs and they are not advised to be used because of
their confusing nature.
Whitespace in C++:
A line containing only whitespace, possibly with a comment, is known as a blank line, and
C++ compiler totally ignores it.
Whitespace is the term used in C++ to describe blanks, tabs, newline characters and
comments. Whitespace separates one part of a statement from another and enables the
compiler to identify where one element in a statement, such as int, ends and the next element
begins. Therefore, in the statement,
int age;
there must be at least one whitespace character (usually a space) between int and age for the
compiler to be able to distinguish them. On the other hand, in the statement
no whitespace characters are necessary between fruit and =, or between = and apples,
although you are free to include some if you wish for readability purpose.
Lab Task no 1
//we are making a program to print hellow world on the screen as an out put.
#include<iostream.h>
#include<conio.h>
void main( );
{
cout<<”hellow world!”<<endl; //Prints hellow world
getch ( );
}
Lab Task No 2
#include <iostream.h>
#include<conio.h>
void main( )
char name;
getch( )}
Lab Task No 3
//we are making a program that performs all arithmetic operations on two variables
#include <iostream.h>
#include<conio.h>
void main( )
{
int a,b;
clrscr;
a=10;
b=5;
cout<<”a+b=”<<a+b<<endl;
cout<<”a-b=”<<a-b<<endl;
cout<<”a*b=”<<a*b<<endl;
cout<<”a/b=”<<a/b<<endl;
getch( );
}
Lab Task No 4
#include<iostream.h>
#include<conio.h.
void main()
{
int a,b,e;
clrscr( );
a=20;
b=20;
e=a*b;
getch( );
}
C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a
keyboard, a disk drive, or a network connection etc. to main memory, this is called input
operation and if bytes flow from main memory to a device like a display screen, a printer, a
disk drive, or a network connection, etc, this is called output operation.
<iostream> This file defines the cin, cout, cerr and clog objects, which correspond to the
standard input stream, the standard output stream, the un-buffered standard
error stream and the buffered standard error stream, respectively.
<iomanip> This file declares services useful for performing formatted I/O with so-called
parameterized stream manipulators, such as setw and set precision.
<fstream> This file declares services for user-controlled file processing. We will discuss
about it in detail in File and Stream related chapter.
Lab Task No 5
Write a program that takes two numbers from users and display their sum in third
variable.
#include<iostream.h>
#include<conio.h>
void main ( )
clrscr ( );
cin>>a;
cin>>b;
c=a+b;
getch( );}
Lab Task No 6
#include<iostream.h>
#include<conio.h>
void main ( )
clrscr ( );
float,area,height,width;
scanf(“%f”,&height);
scanf(“%f”,& width);
area=.5*height*width;
getch ( );
Lab Task No 7
#include<conio.h>
void main ( )
clrscr ( );
int num;
char charc;
cout<<”enter character:”;
cin>>charc;
num=charc;
getch ( );
Lab Task No 8
#include<conio.h>
void main ( )
clrscr ( );
float physics,chemistry,maths,avg,per,sum,total;
cin>>maths;
cin>>chemistry;
cin>>physics;
avg=((physics+chemistry+maths)/3;
cout<<”avg is”<<avg<<endl;
per=((physics+chemistry+maths)/300)*100;
getch ( );
LAB SESSION