Pranto C++ Assignment-1
Pranto C++ Assignment-1
Example : 1
This program outputs a string, two integer values, and a double floating-point value:
#include<iostream>
int main ()
int i, j;
double d;
i = 10;
j = 20;
d = 99.101;
cout << i;
cout << j;
cout << d;
return 0;
Output:
Example-2
int main ()
int i;
cin >> i;
cout<<" Name: PRANTO MALAKER ID.409" <<endl<<"Here is your number : " << i << "\n";
return 0;
Output:
Example-3
The next program prompts the user for an integer value, a floating-point value, and a
#include<iostream>
int main ()
int i;
float f;
char s [80];
return 0;
Output:
Example-3
#include<iostream>
int main ()
char ch;
do
return 0;
Output:
EXERCISES
1. Write a program that inputs the number of hours that an employee works and the em- ployee’s wage.
Then display the employee’s gross pay. (Be sure to prompt for input.)
#include <iostream>
int main() {
cout << "The employee's gross pay is: $" << grossPay << endl;
return 0;
Output:
2. Write a program that converts feet to inches. Prompt the user for feet and display the equivalent
number of inches. Have your program repeat this process until the user enters 0 for the number of feet.
#include <iostream>
int main() {
do {
if (feet != 0) {
cout << feet << " feet is equal to " << inches << " inches." << endl;
return 0;
OUTPUT:
3. #include <iostream>
using namespace std;
int main() {
int a, b, d, min;
min = a > b ? b : a;
break;
if (d == min) {
} else {
cout << "The lowest common denominator is " << d << endl;
return 0;
Output:
EXAMPLES-1.4
#include<iostream>
int num ;
else
return 0;
Output:
EXAMPLES 1.5
1. As a simple first example, this program demonstrates myclass, described in the text. It
sets the value of a of ob1 and ob2 and then displays a’s value for each object:
#include<iostream>
class myclass
int a;
public :
};
a = num;
return a;
int main ()
return 0;
Output:
2. #include<iostream>
class myclass
{
public :
int a;
};
int main ()
ob1 .a = 55;
ob2 .a = 300;
return 0;
Output:
3. To get a taste of the power of objects, let’s look at a more practical example. This programcreates a
class called stack that implements a stack that can be used to store characters:
#include <iostream>
#define SIZE 10
class Stack {
public:
char stck[SIZE];
int tos;
void init() {
tos = 0;
if (tos == SIZE) {
return;
stck[tos++] = ch;
char pop() {
if (tos == 0) {
tos--;
return stck[tos];
};
int main() {
s1.init();
s2.init();
s1.push('a');
s2.push('x');
s1.push('b');
s2.push('y');
s1.push('c');
s2.push('z');
return 0;
Output:
EXERCISES
2. Create a class called card that maintains a library card catalog entry. Have the class store a book’s
title, author, and number of copies on hand. Store the title and author as strings and the number on hand
as an integer. Use a public member function called store() to store a book’s information and a public
member function called show() todisplay the information. Include a short main() function to demonstrate
the class.
#include <iostream>
#include <string>
class Card {
public:
string title;
string author;
int numOnHand;
void store() {
getline(cin, title);
getline(cin, author);
cin.ignore();
void show() {
};
int main() {
Card card1;
card1.store();
card1.show();
return 0;
}
Output:
EXAMPLES
This short C++ program will not compile because the function sum() is not prototyped:
#include<iostream>
int main ()
int a, b, c;
c = sum(a, b);
return 0;
return a+b;
Output:
Here is a short program that illustrates how local variables can be declared anywhere
within a block:
#include<iostream>
int main ()
int i;
cin >> i;
fact = fact * j;
return 0;
Output:
4. The following program creates a Boolean variable called outcome and assigns it the value false. It then
uses this variable in an if statement.
#include<iostream>
int main ()
bool outcome ;
outcome = false ;
if( outcome )
else
return 0;
Output:
EXERCISES
1. The following program will not compile as a C++ program. Why not?
int main ()
f();
return 0;
void f()
The provided code will not compile as a C++ program because the f function is declared after it's called in the
main function. In C++, functions must be defined before they are used. This is known as a forward declaration
issue.
#include <iostream>
void f();
int main() {
f();
return 0;
void f() {
Output:
2.Here is another example of function overloading. In this case, the function date() is overloaded to accept
the date either as a string or as three integers. In both cases, the function displays the data passed to it.
#include<iostream>
using namespace std;
int main ()
date (8 , 23 , 99) ;
return 0;
// Date as string .
// Date as integers .
Output:
3. So far, you have seen overloaded functions that differ in the data types of their arguments. However,
overloaded functions can also differ in the number of arguments, as this example illustrates:
#include<iostream>
int main ()
f1 (10) ;
f1 (10 , 20) ;
return 0;
void f1(int a)
Output:
3. Create a function called min() that returns the smaller of the two numeric arguments used to call the
function. Overload min() so it accepts characters, integers, and doubles as arguments.
#include <iostream>
return (a < b) ? a : b;
return (a < b) ? a : b;
return (a < b) ? a : b;
int main() {
cout << "Minimum character: " << min('x', 'y') << endl;
cout << "Minimum integer: " << min(10, 25) << endl;
cout << "Minimum double: " << min(3.14, 2.71) << endl;
return 0;
Output:
Write a program that uses C++-style I/O to input two integers from the keyboard and then displays the
result of raising the first to the power of the second. (For example, if the user enters 2 and 4, the result is
24, or 16.)
#include <iostream>
int main() {
result *= base;
cout << base << " raised to the power of " << exponent << " is " << result << endl;
return 0;
Output:
4. Create a function called rev str() that reverses a string. Overload rev str() so it can be called with either
one character array or two. When it is called with one string, have that one string contain the reversal.
When it is called with two strings, return the reversed string in the second argument.
#include <iostream>
#include <cstring>
strcpy(str2, str1);
rev_str(str2);
int main() {
strcpy(s1, "hello");
rev_str(s1, s2);
rev_str(s1);
return 0;
Output:
5. Given the following new-style C++ program, show how to change it into its old-style form. 5. Given the
following new-style C++ program, show how to change it into its old-style form.
#include<iostream>
int main ()
return 0;
int f(int a)
return a * 3.1416;
Output:
CHAPTER-2
A constructor function has the same name as the class of which it is a part and has no return type. For
example, here is a short class that contains a constructor function:
#include<iostream>
class myclass
int a;
public :
myclass ();
myclass :: myclass ()
a = 10;
cout << a;
int main ()
myclass ob;
return 0;
Output:
#include <iostream>
class MyClass {
public:
MyClass() {
cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";
a = 10;
~MyClass() {
void show() {
private:
int a;
};
int main() {
MyClass ob;
ob.show();
return 0;
Output:
Create a class called t and d that is passed the current system time and date as a parameter to its
constructor when it is created. Have the class include a member function that displays this time and date
on the screen. (Hint: Use the standard time and date functions found in the standard library to find and
display the time and date.)
#include <iostream>
#include <ctime>
using namespace std;
class t_and_d {
private:
time_t currentTime;
public:
t_and_d(time_t time) : currentTime(time) {
void display_time_and_date() {
cout << "Current time and date: "
<< asctime(localtime(¤tTime));}
};
int main() {
cout<<"solaiman 275"<<endl;
time_t now = time(nullptr);
t_and_d obj(now);
obj.display_time_and_date();
return 0;
Create a class called box whose constructor function is passed three double values, each of which represents
the length of one side of a box. Have the box class compute the volume of the box and store the result in a
doublevariable. Include a member function called vol() that displays the volume of each box object.
#include <iostream>
class box {
private:
double volume;
public:
void vol() {
cout << "Volume of the box: "
};
int main() {
box1.vol();
box2.vol();
return 0;
Output:
class area_cl
{
public :
double height ;
double width ;
};
create two derived classes called rectangle and isosceles that inherit area cl. Have each class include a
function called area() that returns the area of a rectangle or isosceles triangle, as appropriate. Use
parameterized constructors to initialize height and width.
Code:
#include <iostream>
public:
double height;
double width;
};
public:
double area() {
public:
double area() {
};
int main() {
cout << "Area of isosceles triangle: " << tri.area() << endl;
return 0;}
Output:
Use a union class to swap the low- and high-order bytes of an integer (assuming 16-bit integers; if your
computer uses 32-bit integers, swap the bytes of a short int).
Code:
#include <iostream>
union {
struct {
} bytes;
} swap;
swap.num = num;
swap(swap.bytes.low, swap.bytes.high);
num = swap.num;
int main() {
cout << "Number after swapping bytes: " << num << endl;
return 0;
Output:
• In Chapter 1 you overloaded the abs() function so that it could find the absolute value of integers, long
integers, and doubles. Modify that program so that those functions are expanded in line.
Code:
#include <iostream>
#include <cstdlib>
int main() {
int a = -10;
long b = -20;
double c = -3.14;
cout<<"name:PRANTO MALAKER;ID:409"<<endl;
cout << "The absolute value of"<< a << " is " << (a >= 0 ? a : -a) << endl;
cout << "The absolute value of" << b << " is " << (b >= 0 ? b : -b) << endl;
cout << "The absolute value of" << c << " is " << (c >= 0.0 ? c : -c) << endl;
return 0;
Output:
• Create a class called line that draws a line on the screen. Store the line length in a private integer
variable called len. Have line’s constructor take one parameter: the line length. Have the constructor
store the length and actually draw the line. If your system does not support graphics, display the line
by using *. Optional: Give linea destructor that erases the line.
Code:
#include <iostream>
#include <string>
class Line {
private:
int len;
public:
Line(int length) :
len(length)
drawLine();
~Line() {
eraseLine(); }
private:
void drawLine() {
void eraseLine() {
int main() {
Line line1(30);
return 0;
Output:
Chapter 3:
1. What is wrong with the following program?
return 0;
#include <iostream>
class cl2;
class cl1
{ int i, j;
public:
void display() {
cout << "i: " << i << ", j: " << j << endl; }};
};
int main() {
cout<<"solaiman 275"<<endl;
x = y;
x.display();
2. To illustrate exactly when an object is constructed and destructed when returned from a function, create a
class called who. Have who’s constructor take one character argument that will be used to identify an object.
Have the constructor display a message similar to this when constructing an object:where x is the identifying
character associated with each object. When an object is destroyed, have a message similar to this displayed:
where, again, x is the identifying character. Finally, create a function called make who() that returns a who
object. Give each object a unique name. Note the output displayed by the program.
Code:
#include <iostream>
class Who {
char identifier;
public:
~Who() {
};
Who makeWho() {
return Who(id++);
int main() {
return 0;
}
OUTPUT:
3. Imagine a situation in which two classes, called pr1 and pr2, shown here, share one printer. Further,
imagine that other parts of your program need to know when the printer is in use by an object of either of
these two classes. Create a function called inuse() that returns true when the printer is being used by either
and false otherwise. Make this function a friend of both pr1 and pr2.
Code:
#include <iostream>
class pr1;
class pr2 {
bool printing;
public:
};
class pr1 {
bool printing;
public:
int main() {
printer1.set_print(true);
cout << "Printer in use: " << boolalpha << inuse(printer1, printer2) << endl;
printer1.set_print(false);
cout << "Printer in use: " << boolalpha << inuse(printer1, printer2) << endl;
return 0;
output:
CHAPTER 3 EXAMPLE:
class myclass
int a, b;public :
void show ()
myclass o1 , o2;
o2 = o1;
return 0;
Output:
2.
class samp
{int i;
public :
samp (int n) { i = n; }
return 0;
}
Output:
3.As stated, the default method of parameter passing in C++, including objects, is by value. This means that a
bitwise copy of the argument is made and it is this copy that is used by the function. Therefore, changes to the
object inside the function do not affect the calling object. This is illustrated by the following example:
return 0;
}
Output:
4.
class samp{
int i;public :
samp a (10) ;
return 0;
Output:
5.
class samp
{char s [80];public :
char s [80];
samp str ;
cin >> s;
return str;}
samp ob;
ob = input ();
return 0;
Output:
6. One common (and good) use of a friend function occurs when two different types of classes have some
quantity in common that needs to be compared. For example, consider the following program, which creates
a class called car and a class called truck, each containing, as a private variable, the speed of the vehicle it
represents:
#include <iostream>
using namespace std;
class Truck;
class Car {
int passengers;
int speed;
public:
Car(int p, int s) { passengers = p; speed = s; }
friend int sp_greater(Car c, Truck t);
};
class Truck {
int weight;
int speed;
public:
Truck(int w, int s) { weight = w; speed = s; }
friend int sp_greater(Car c, Truck t);
};
int main() {
int t;
Car c1(6, 55), c2(2, 120);
Truck t1(10000, 55), t2(20000, 72);
return 0;
}
Output:
7.
#include <iostream>
using namespace std;
class myclass {
int n, d;
public:
myclass(int i, int j) { n = i; d = j; }
friend int isfactor(myclass ob);
};
int main() {
if (isfactor(ob1))
cout << "2 is a factor of 10\n";
else
cout << "2 is not a factor of 10\n";
if (isfactor(ob2))
cout << "3 is a factor of 13\n";
else
cout << "3 is not a factor of 13\n";
return 0;
}
Output: