Sunteți pe pagina 1din 7

Practical on Variables and Operators

1. The following program uses the * operator to compute the area of a rectangle given its length and the
width.

2. The previous program is rewritten to allow the users to enter the dimensions of the rectangle:

1 (21.01.2019, 25.01.2019)
3. Conversion of feet to meter:
#include <iostream>
using namespace std;
int main()
{
double f; // holds the length in feet
double m; // holds the conversion to meters
cout << "Enter the length in feet: ";
cin >> f; // read the number of feet
m = f / 3.28; // convert to meters
cout << f << " feet is " << m << " meters.";

return 0;

4. Demonstrating the Boolean datatype:

2 (21.01.2019, 25.01.2019)
5. An XOR operation program that demonstrates the XOR operation for all four possible combinations
of true/false values.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
bool p, q;
p = true;

q = true;
cout << p << " XOR " << q << " is " <<
( (p || q) && !(p && q) ) << "\n";
p = false;
q = true;
cout << p << " XOR " << q << " is " <<
( (p || q) && !(p && q) ) << "\n";
p = true;
q = false;
cout << p << " XOR " << q << " is " <<
( (p || q) && !(p && q) ) << "\n";
p = false;
q = false;
cout << p << " XOR " << q << " is " <<
( (p || q) && !(p && q) ) << "\n";
return 0;
}

Output:

1 XOR 1 is 0
0 XOR 1 is 1
1 XOR 0 is 1
0 XOR 0 is 0

Working with Functions

6. A program to understand the workflow of a C++ code:


#include <iostream>
using namespace std;
void line(), message(); // Prototypes
int main()
{
cout << "Hello! The program starts in main()."
<< endl;
line();
message();
line();
cout << "At the end of main()." << endl;
return 0;
}
void line() // To draw a line.
{

3 (21.01.2019, 25.01.2019)
cout << "--------------------------------" << endl;
}
void message() // To display a message.
{
cout << "In function message()." << endl;
}

Output:
Hello! The program starts in main().
-----------------------------------
In function message().
-----------------------------------
At the end of main().

Explanation:

The example on the opposite page shows the structure of a C++ program containing multiple functions.
In C++, functions do not need to be defined in any fixed order. For example, we could define the
function message() first, followed by the function line(), and finally the main() function.

However, it is more common to start with the main() function as this function controls the program
flow. In other words, main() calls functions that have yet to be defined. This is made possible by
supplying the compiler with a function prototype that includes all the information the compiler needs.

7. C++ program to show the letters of a name using String class/ String library:

#include <iostream> // Declaration of cin, cout


#include <string> // Declaration of class string
using namespace std;
int main()
{
// Defines four strings:
string prompt("What is your name: "),
name, // An empty
line( 40, '-'), // string with 40 '-'
total = "Hello "; // is possible!
cout << prompt; // Request for input.
getline( cin, name); // Inputs a name in one line
total = total + name; // Concatenates and
// assigns strings.
cout << line << endl // Outputs line and name
<< total << endl;
cout << " Your name is " // Outputs length
<< name.length() << " characters long!" << endl;
cout << line << endl;

4 (21.01.2019, 25.01.2019)
return 0;
}

8. C++ program to compute the square roots of a double, an integer and a long type of variable within
the same program:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x1 = 44.22415;
int x2 = 25;
long x3 = 34870;
cout << "\n Number \t Square Root" << endl;
cout << "\n " << x1 << " \t " << sqrt(x1)
<< "\n " << x2 << " \t " << sqrt(x2)
<< "\n " << x3 << " \t " << sqrt(x3) << endl;
return 0;
}

9. Inline functions:

One of the objectives of using functions in a program is to save Some memory space. Which becomes
appreciable when a function is likely to be called many times. However, every time a function it; called,
it takes a lot of extra time in executing a series of instructions for tasks such as jumping to the function,
saving registers, pushing arguments into the stack, and returning to the calling function. When a
function is small, a substantial percentage of execution time may be spent in such overheads.

C++ has a different solution to this problem. To eliminate the cost of calls to small functions, C++
proposes a new feature called inline function. An inline function is a function that is expanded in line
when it is invoked. That is, the compiler replaces the function call with the corresponding function code
(something similar to macros expansion). The inline functions are defined as follows:

5 (21.01.2019, 25.01.2019)
Sample program to demonstrate inline function in C++:

10. Function overloading in C++:

In C++, it is possible to define several functions within a class or within the main( ) function with the
same name, as long as they differ in their numbers and/or types of arguments (or parameters).
This method of defining two or more methods with the same name in a class or main( ) is called as
“Function Overloading”, and the functions are said to be “overloaded”.
Function overloading is one of the many ways that that C++ supports polymorphism. Function
overloading allows us to reuse the same function name repeatedly, but with different types and/or
number of arguments and parameters.

Example 1:

#include <iostream>
using namespace std;

void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}

int main() {

6 (21.01.2019, 25.01.2019)
print(10);
print(10.10);
print("ten");
return 0;
}

Example 2:

#include<iostream>
using namespace std;

int main ()
{
int cube (int);
float cube(float);
int a, a_cube;
float b, b_cube;

cout << "Enter a number : \n";


cin >> a;
cout << "Enter floating point number : \n";
cin >> b;

a_cube = cube(a);
cout << "a : " << a;
cout << "\ncube is " << a_cube;

b_cube = cube(b);
cout << "\nb : " << b;
cout << "\ncube is " << b_cube;
return 0;
}

int cube(int a)
{
return (a*a*a);
}

float cube(float b)
{
return (b*b*b);
}

____________________

7 (21.01.2019, 25.01.2019)

S-ar putea să vă placă și