Lab Report 2
Lab Report 2
Lab Report 02
1ST SEMESTER
Theory :
1. Structure of C++ Program
2. Introduction to classes in C++
3. Difference b/w “char” and “string”
• Preprocessing Directives :
They give instructions before the
program is compiled.
It begins with #include and then file name
inside < > signs such as
#include<file name>
Example :
#include<iostream> is a preprocessing directive
to give additional statements regarding input/output.
#include“filename”
#include<filename>
• Directives :
Tells compiler to use library names declared in
namespace .
Example:
std (standard namespace) has C++ defined components.
• Main Function :
Every C++ Program contains set of instructions
which constitute the main program called the main function.
Example :
int main shows that function will return integer value to
the system after execution.
The main function is always enclosed inside braces { } called code
blocks.
• Declarations :
Before storing data inside a variable, we have to
first allocate it a memory location that will be used by it to store input
user data, this task is performed by writing statement called
Declaration.
Example :
int a ;
float b ;
Here “int a” means that variable a can only store integer values, and
“float b” means b can store values that are in decimal points.
• Assignment Statements :
Such statements that are written to
assign values to variables are called assignment statements.
These values can be a character, an integer or result of a program.
Example :
a=b
t=0
x = y = z = ans
• Arithmatic Operators :
They are the mathematical characters
used for different math functions i.e + - / * etc .
• Data Types :
They are the strings which tell us which type of data
will be stored in the variable.
The common C++ data types are
int : stores only integer values, takes up the size of 4 bytes.
float : stores data in decimal values, takes up the size of 4 bytes.
char : stores data in form of single character(letter or number);
takes up the size of 1 byte.
bool : stores data in form of Boolean variables (True/False)
double : similar data type as float with higher storage capacity
i.e 8 bytes.
2. Introduction to Classes :
• Definition :
Classes are mainly used to define user defined data
types. Classes make the new data easily accessible and can be
implemented to other functions without much effort,
• Declaration :
• Implementation :
The constructor file must include block of
every method declared in class. They have the same name as that
of the class.
In this way, a single class can give rise to multiple
objects (programs), as different programs may use the data of the
class according to their needs.
3. Difference b/w string and char :
A char is just a letter, an alphabet that may also be a number.
While on the other hand, a string is an array of chars i.e it can be
a full name unlike char.
1. In char, you can only take one character at a time while in string
you can store a complete word in the defined variable.
#include<iostream>
#include<cmath>
float dist;
cout<<"Please put in Px :";//input and printing of variables
cin>>px;
cout<<"Please put in Py :";
cin>>py;
ans=sqrt(dist);
Home Task :
1. Program to convert Miles into kilometers:
2. //**************************Convert Miles into
Kilometres****************************//
3. #include<iostream>
4. using namespace std;
5.
6. int main(){
7. float mil,km;
8. cout<<"Enter the value in Miles :";
9. cin>>mil;
10. cout<<"The value in Miles is "<<mil<<" mil.";
11. km=0.62137*mil;
12. cout<<"\nConverting into Kilometres........";
13. cout<<"\nThe value in Kilometres is "<<km<<" km.";
14.
2. Program to convert Meters into Miles:
3. //**************************Convert Metres into
Miles****************************//
4. #include<iostream>
5. using namespace std;
6.
7. int main(){
8. float mil,m;
9. cout<<"Enter the value in Metres :";
10. cin>>m;
11. cout<<"The value in Metres is "<<m<<" m.";
12. mil=0.00062137*m;
13. cout<<"\nConverting into Miles........";
14. cout<<"\nThe value in Miles is "<<mil<<" mil.";
15. }
int main(){
float cel,rk;
cout<<"Enter the value in Celcius :";
cin>>cel;
cout<<"The value in Celcius is "<<cel<<" *C.";
rk=1.8*(cel)+491.67;
cout<<"\nConverting into Rankines........";
cout<<"\nThe value in Rankines is "<<rk<<" degrees rankine.";
}
Class Implementation:
/*------------------------------------------------------*/
/* Class implementation for Point */
/* filename: Point.cpp */
#include "Point.h" //Required for Point
#include <iostream> //Required for cout
using namespace std;
//Parameterized constructor
P:: P(double x, double y)
{