[go: up one dir, main page]

0% found this document useful (0 votes)
38 views7 pages

Zain Lab Report 3

The document discusses three topics: 1. The getline function, which allows reading multiple lines of input unlike cin, avoiding issues with spaces. 2. Increment operators, which can increment a variable's value before or after assignment using ++ or ++. 3. Classes and objects in C++, which allow organizing code and data into reusable chunks through classes and instantiating objects from those classes. The lab work provides code examples demonstrating the use of getline, increment operators, and defining a class with multiple methods to showcase objects.

Uploaded by

Mustafa Siddiqui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views7 pages

Zain Lab Report 3

The document discusses three topics: 1. The getline function, which allows reading multiple lines of input unlike cin, avoiding issues with spaces. 2. Increment operators, which can increment a variable's value before or after assignment using ++ or ++. 3. Classes and objects in C++, which allow organizing code and data into reusable chunks through classes and instantiating objects from those classes. The lab work provides code examples demonstrating the use of getline, increment operators, and defining a class with multiple methods to showcase objects.

Uploaded by

Mustafa Siddiqui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Fundamentals of Programming

Lab Report 03

Introduction to Getline ,Increment and Classes


1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: ME-14 Section: C Group: -
SUBMITTED BY
Name CMS Objective Theory Lab Lab
(1) Work(3) Total
(3) Task (3)
M.Zain.ul.Abideen 408541

School of Mechanical and Manufacturing Engineering


Objectives:
 To learn about getline function.
 To learn about increment operator.
 To learn about classes and objects.

Theory:
1. Getline function.
2. Increment Operator.
3. Classes and Objects.

Getline Function:
The getline function is a built in function in the standard C++ library that is
often used to take input in a better and more professional way than the cin function because
the cin function takes spaces as an enter and doesnt let you write a whole line in the input
terminal while the getline function allows you to read multiple lines of strings from the imput
stream. e.g
We initialize a variable.
int a;
getline(cin,a);

This is the syntax of how the getline function can be used in our
programs.

Increment Operator:
Increment operators are used to increase the value of a variable by 1 , It
works in 2 way ,either increasing value before assigning or after assigning. Its syntax can be as
shown:
int a;
a++; for post increment
++a for pre increment
Post increment increases the value after assiging it the variable and pre increment increases it
before assigning it to the variable.

Classes and Objects:


Classes in C++ are building blocks that eventually lead to object oriented
coding and is useful in C++ because it allows the user to grab a big chunk of code and place it
anywhere the user wants for convinience and is often followed by the command public: and its
syntax is almost similar to the main function.It also makes streamline code easy to read.

Lab work:
Code to use Getline function:
#include <iostream> //header file
using namespace std;
int main() {
string ans;
cout << "What do you like man? ";
//getline(cin,ans);
cin >> ans;
cout << ans;
return 0;
}

In this function we use cin function and in the ouput we can see that it only registers the string
before the space.
Output:

Now in the following program we getline:


#include <iostream> //header file
using namespace std;
int main() {
string ans;
cout << "What do you like man? ";
getline(cin,ans);
//cin >> ans;
cout << ans;
return 0;
}

Output:

Code to Increment a value:


For post increment:
#include <iostream> //header file
using namespace std;
int main() {
int a, b;
a = 1;
b = a++;
cout << "a= " << a << endl << "b= " << b;
return 0;
}

Output:
For pre increment:
#include <iostream> //header file
using namespace std;
int main() {
int a, b;
a = 1;
b = ++a;
cout << "a= " << a << endl << "b= " << b;
return 0;
}

Output:

LAB TASK ON NEXT PAGE;


Lab Task:
Code for Classes and Objects:
#include <iostream>
using namespace std;
class header {
public:
double code_1(){
cout<< "Hi this is the first code for class" << endl;
return 0;
}
double code_2() {
int x,y;
x=1 , y=2 ;
cout << x + y << endl;
return 0;
}
double code_3(){
double a,b,c;
a = 2;
b = 5;
c = (a*.2) + (b*.4);
cout << c << endl;
return 0;
}
double code_4() {
string name;
cout<<"What is your name?";
cin>>name;
cout << "Mubarak ho " << name << " apka rishta pakka!";
return 0;
}
int main () {
code_3();
}
};

int main() {
header headobj;
headobj.code_1();
headobj.code_2();
headobj.code_3();
headobj.code_4();
return 0;
}
Output:

Getline Solution:
Adding >> ws after cin in the syntax of getline resolves the faced problem .

#include <iostream>
using namespace std;
int main() {
string ans;
int a;
cout << "Type a number: ";
cin >> a;
cout << "How is the weather today? ";
getline(cin >> ws, ans);
cout << ans << endl;
cout << "Its " << a << " degrees today.";
return 0;
}

Output:

You might also like