0% found this document useful (0 votes)
47 views41 pages

Pranto C++ Assignment-1

The document contains examples of C++ programs that demonstrate various concepts like functions, classes, objects, and overloading. It also contains exercises for practicing these concepts. The examples include programs that output values, prompt user input, define and use classes and objects, comment code, declare variables in different scopes, and overload functions based on data types and number of arguments. The exercises prompt the user to create programs applying these same concepts, like defining a class to represent a library catalog entry.

Uploaded by

Mredul Adhikary
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)
47 views41 pages

Pranto C++ Assignment-1

The document contains examples of C++ programs that demonstrate various concepts like functions, classes, objects, and overloading. It also contains exercises for practicing these concepts. The examples include programs that output values, prompt user input, define and use classes and objects, comment code, declare variables in different scopes, and overload functions based on data types and number of arguments. The exercises prompt the user to create programs applying these same concepts, like defining a class to represent a library catalog entry.

Uploaded by

Mredul Adhikary
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/ 41

CHAPTER-1

Example : 1

This program outputs a string, two integer values, and a double floating-point value:

#include<iostream>

using namespace std;

int main ()

int i, j;

double d;

i = 10;

j = 20;

d = 99.101;

cout<<"Name: PRANTO MALAKER ID:409"<<endl;

cout << " Here are some values : ";

cout << i;

cout << " ";

cout << j;

cout << " ";

cout << d;

return 0;

Output:

Example-2

This program prompts the user for an integer value:


#include<iostream>

using namespace std;

int main ()

int i;

cout << " Enter a value : ";

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

string. It then uses one input statement to read all three.

#include<iostream>

using namespace std;

int main ()

int i;

float f;

char s [80];

cout<<" Name: PRANTO MALAKER ID.409" <<endl;

cout << " Enter an integer , float , and string : ";


cin >> i >> f >> s;

cout << "Here's your data : " ;

cout << i <<" "<< f <<" "<< s;

return 0;

Output:

Example-3

#include<iostream>

using namespace std;

int main ()

cout<<" Name: PRANTO MALAKER ID.409 " <<endl;

char ch;

cout << "Enter keys , x to stop .\n";

do

cout << ": ";

cin >> ch;

while (ch !='x');

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>

using namespace std;

int main() {

cout<<" Name: PRANTO MALAKER ID.409 " <<endl;

double hoursWorked, wage, grossPay;

cout << "Enter the number of hours worked: ";

cin >> hoursWorked;

cout << "Enter the employee's wage: ";

cin >> wage;

grossPay = hoursWorked * wage;

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>

using namespace std;

int main() {

int feet, inches;

cout<<" Name:PRANTO MALAKER ID.409 " <<endl;

do {

cout << "Enter the number of feet (0 to quit): ";

cin >> feet;

if (feet != 0) {

inches = feet * 12;

cout << feet << " feet is equal to " << inches << " inches." << endl;

} while (feet != 0);

cout << "Thank you" << endl;

return 0;

OUTPUT:

3. #include <iostream>
using namespace std;

int main() {

int a, b, d, min;

cout<<" Name: PRANTO MALAKER ID:409 " <<endl;

cout << "Enter two numbers: ";

cin >> a >> b;

min = a > b ? b : a;

for (d = 2; d < min; d++) {

if ((a % d == 0) && (b % d == 0)) {

break;

if (d == min) {

cout << "No common denominators\n";

} else {

cout << "The lowest common denominator is " << d << endl;

return 0;

Output:

EXAMPLES-1.4

1. Here is a program that contains both C and C++-style comments:

#include<iostream>

using namespace std;


int main ()

int num ;

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

cout << "Enter number to be tested : ";

cin >> num ;

if (( num %2) ==0)

cout << " Number is even \n";

else

cout << " Number is odd \n";

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>

using namespace std;

class myclass

int a;

public :

void set_a (int num );


int get_a ();

};

void myclass :: set_a (int num )

a = num;

int myclass :: get_a ()

return a;

int main ()

myclass ob1 , ob2 ;

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

ob1 . set_a (10) ;

ob2 . set_a (99) ;

cout << ob1 . get_a () << "\n";

cout << ob2 . get_a () << "\n";

return 0;

Output:

2. #include<iostream>

using namespace std;

class myclass
{

public :

int a;

};

int main ()

myclass ob1 , ob2 ;

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

ob1 .a = 55;

ob2 .a = 300;

cout << ob1 .a << "\n";

cout << ob2 .a << "\n";

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>

using namespace std;

#define SIZE 10

class Stack {

public:

char stck[SIZE];
int tos;

void init() {

tos = 0;

void push(char ch) {

if (tos == SIZE) {

cout << "Stack is full\n";

return;

stck[tos++] = ch;

char pop() {

if (tos == 0) {

cout << "Stack is empty\n";

return '\0'; // Or handle the empty stack case differently

tos--;

return stck[tos];

};

int main() {

cout<<"NAME: PRANTO MALAKER "<<" "<<"ID:409\n";

Stack s1, s2;

s1.init();

s2.init();

s1.push('a');

s2.push('x');

s1.push('b');
s2.push('y');

s1.push('c');

s2.push('z');

for (int i = 0; i < 3; i++) {

cout << "Pop s1: " << s1.pop() << endl;

for (int i = 0; i < 3; i++) {

cout << "Pop s2: " << s2.pop() << endl;

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>

using namespace std;

class Card {
public:

string title;

string author;

int numOnHand;

void store() {

cout<<"Name:PRANTO MALAKER "<<" "<<"ID:275\n";

cout << "Enter book title: ";

getline(cin, title);

cout << "Enter author name: ";

getline(cin, author);

cout << "Enter number of copies on hand: ";

cin >> numOnHand;

cin.ignore();

void show() {

cout << "Title: " << title << endl;

cout << "Author: " << author << endl;

cout << "Number on hand: " << numOnHand << endl;

};

int main() {

Card card1;

card1.store();

cout << "\nBook information:" << endl;

card1.show();

return 0;

}
Output:

EXAMPLES

This short C++ program will not compile because the function sum() is not prototyped:

#include<iostream>

using namespace std;

int main ()

int a, b, c;

cout << " Enter two numbers : ";

cin >> a >> b;

c = sum(a, b);

cout << "Sum is: " << c;

return 0;

sum (int a, int b)

return a+b;

Output:
Here is a short program that illustrates how local variables can be declared anywhere

within a block:

#include<iostream>

using namespace std;

int main ()

int i;

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

cout << "Enter number : ";

cin >> i;

int j, fact =1;

for (j=i; j >=1; j--){

fact = fact * j;

cout << " Factorial is " << fact ;

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>

using namespace std;

int main ()

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

bool outcome ;

outcome = false ;

if( outcome )

cout << " true ";

else

cout << "false ";

return 0;

Output:

EXERCISES

1. The following program will not compile as a C++ program. Why not?

// This program has an error .

# include <iostream >

using namespace std;

int main ()

f();
return 0;

void f()

cout << " this won ’t work ";

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.

Here's the fixed version of the code:

#include <iostream>

using namespace std;

void f();

int main() {

f();

return 0;

void f() {

cout<<"Name: PRANTO MALAKER "<<" "<<"ID:409\n";

cout << "NOW ITS CORRECT!";

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;

void date ( char * date );

void date (int month , int day , int year );

int main ()

date (" 8/23/99 ");

date (8 , 23 , 99) ;

return 0;

// Date as string .

void date ( char * date )

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

cout << "Date : " << date << "\n";

// Date as integers .

void date (int month , int day , int year )

cout << "Date : " << month << "/";

cout << day << "/" << year << "\n";

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>

using namespace std;

void f1(int a);

void f1(int a, int b);

int main ()

f1 (10) ;

f1 (10 , 20) ;

return 0;

void f1(int a)

cout<<"Name:PRANTO MALAKER "<<" "<<"ID:409\n";

cout << "In f1(int a)"<<" "<<a<<endl;

void f1(int a, int b)

cout << "In f1(int a, int b)"<<a<<","<<b;

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>

using namespace std;


char min(char a, char b) {

return (a < b) ? a : b;

int min(int a, int b) {

return (a < b) ? a : b;

double min(double a, double b) {

return (a < b) ? a : b;

int main() {

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

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>

using namespace std;

int main() {

int base, exponent;


long long result = 1;

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

cout << "Enter the base: ";

cin >> base;

cout << "Enter the exponent: ";

cin >> exponent;

for (int i = 0; i < exponent; i++) {

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>

using namespace std;

void rev_str(char str[]) {

int len = strlen(str);

for (int i = 0; i < len / 2; i++) {

char temp = str[i];

str[i] = str[len - i - 1];


str[len - i - 1] = temp;

void rev_str(char str1[], char str2[]) {

strcpy(str2, str1);

rev_str(str2);

int main() {

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

char s1[80], s2[80];

strcpy(s1, "hello");

rev_str(s1, s2);

cout << "s1: " << s1 << endl;

cout << "s2 (reversed): " << s2 << endl;

rev_str(s1);

cout << "s1 (reversed): " << s1 << endl;

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>

using namespace std;


int f(int a);

int main ()

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

cout << f (10) ;

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>

using namespace std;

class myclass

int a;

public :

myclass ();

void show ();


};

myclass :: myclass ()

cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

cout << "In constructor \n";

a = 10;

void myclass :: show ()

cout << a;

int main ()

myclass ob;

ob. show ();

return 0;

Output:

#include <iostream>

using namespace std;

class MyClass {

public:

MyClass() {
cout<<"Name: PRANTO MALAKER"<<" "<<"ID:409\n";

cout << "In constructor\n";

a = 10;

~MyClass() {

cout << "Destructing...\n";

void show() {

cout << a << "\n";

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(&currentTime));}
};

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>

using namespace std;

class box {

private:

double length, width, height;

double volume;

public:

box(double l, double w, double h)

: length(l), width(w), height(h) {

volume = length * width * height;

void vol() {
cout << "Volume of the box: "

<< volume << endl;

};

int main() {

cout<<"PRANTO MALAKER 409"<<endl;

box box1(6.0, 3.0, 14.0);

box1.vol();

box box2(65.0, 26.0, 47.0);

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>

using namespace std;


class area_cl {

public:

double height;

double width;

area_cl(double h, double w) : height(h), width(w) {}

};

class rectangle : public area_cl {

public:

rectangle(double h, double w) : area_cl(h, w){}

double area() {

return height * width; }};

class isosceles : public area_cl {

public:

isosceles(double h, double w) : area_cl(h, w) {}

double area() {

return 0.5 * height * width; }

};

int main() {

cout<<"name:solaiman id: 275 "<<endl;

rectangle rect(44, 95);

cout << "Area of rectangle: " << rect.area() << endl;

isosceles tri(74, 25);

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>

using namespace std;

union {

short int num;

struct {

unsigned char low;

unsigned char high;

} bytes;

} swap;

void swap_bytes(int& num) {

swap.num = num;

swap(swap.bytes.low, swap.bytes.high);

num = swap.num;

int main() {

cout<<”PRANTO MALAKER 409”<<endl;

int num = 0x1234;

cout << "Original number: " << num << endl;


swap_bytes(num);

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>

using namespace std;

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>

using namespace std;

class Line {

private:

int len;

public:

Line(int length) :

len(length)

drawLine();

~Line() {

eraseLine(); }

private:

void drawLine() {

for (int i = 0; i < len; ++i) {


cout << "*";

cout << endl;}

void eraseLine() {

for (int i = 0; i < len; ++i) {

cout << "\b \b"; }

cout << endl;}};

int main() {

cout<<"PRANTO MALAKER 409"<<endl;

Line line1(30);

cout << "Line is about to be destroyed..." << endl;

return 0;

Output:

Chapter 3:
1. What is wrong with the following program?

// This program has an error .

# include <iostream >


using namespace std;
class cl1
{
int i, j;
public :
cl1 (int a, int b) { i = a; j = b; }
// ...
};
class cl2
{
int i, j;
public :
cl2 (int a, int b) { i = a; j = b; }
// ...
};
int main ()
{
cl1 x(10 , 20) ;
cl2 y(0 , 0);
x = y;

return 0;

} THE CORRECT CODE:

#include <iostream>

using namespace std;

class cl2;

class cl1

{ int i, j;

public:

cl1(int a, int b) { i = a; j = b; } cl1(const cl2& obj);

void display() {

cout << "i: " << i << ", j: " << j << endl; }};

class cl2 { int i, j;

public: cl2(int a, int b) { i = a; j = b; }

friend class cl1;

};

cl1::cl1(const cl2& obj) {i=obj.i;j=obj.j;}

int main() {

cout<<"solaiman 275"<<endl;

cl1 x(10, 20);

cl2 y(0, 0);

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>

using namespace std;

class Who {

char identifier;

public:

Who(char id) : identifier(id) {

cout << "Constructing who #" << identifier << endl;

~Who() {

cout << "Destroying who #" << identifier << endl;}

};

Who makeWho() {

static char id = 'A';

return Who(id++);

int main() {

Who obj1 = makeWho();

Who obj2 = makeWho();

Who obj3 = makeWho();

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>

using namespace std;

class pr1;

class pr2 {

bool printing;

friend bool inuse(const pr1& obj1, const pr2& obj2);

public:

pr2() { printing = false; }

void set_print(bool status) { printing = status; }

};

class pr1 {

bool printing;

friend bool inuse(const pr1& obj1, const pr2& obj2);

public:

pr1() { printing = false; }


void set_print(bool status) { printing = status; }};

bool inuse(const pr1& obj1, const pr2& obj2) {

return (obj1.printing || obj2.printing);}

int main() {

pr1 printer1; pr2 printer2;

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:

# include <iostream >

using namespace std;

class myclass

int a, b;public :

void set (int i, int j) { a = i; b = j; }

void show ()

{ cout << a << ' ' << b << "\n"; }};


int main ()

{cout<<"PRANTO MALAKER 409"<<endl;

myclass o1 , o2;

o1. set (10 , 4);

o2 = o1;

o1. show ();

o2. show ();

return 0;

Output:

2.

# include <iostream >

using namespace std;

class samp

{int i;

public :

samp (int n) { i = n; }

int get_i () { return i; }};

int sqr_it ( samp o){

return o. get_i () * o. get_i ();}

int main (){samp a (10) , b(2) ;

cout<<"PRANTO MALAKER 409 "<<endl;

cout << sqr_it (a) << "\n";

cout << sqr_it (b) << "\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:

# include <iostream >


using namespace std;
class samp
{
int i;
public :
samp (int n) { i = n; }
void set_i (int n) { i = n; }
int get_i () { return i; }
};

void sqr_it ( samp o)


{
o. set_i (o. get_i () * o. get_i ());
cout << " Copy of a has i value of "
<< o. get_i ()<<endl;}
int main (){
samp a (10) ;
sqr_it (a);
cout<<"PRANTO MALAKER 409 "<<endl;
cout << "But , a.i is unchanged in main : ";
cout << a. get_i ()<<endl;

return 0;
}

Output:

4.

# include <iostream >

using namespace std;

class samp{
int i;public :

samp (int n){i = n;

cout << " Constructing \n";}

~samp (){ cout << " Destructing \n"; }

int get_i () { return i; }

};int sqr_it ( samp o){

return o. get_i () * o. get_i ();

int main (){

samp a (10) ;

cout<<"PRANTO MALAKER 409”<<endl";

cout << sqr_it (a) << "\n";

return 0;

Output:

5.

# include <iostream >

# include <cstring >

using namespace std;

class samp

{char s [80];public :

void show () { cout << s << "\n"; }

void set ( char *str )

{ strcpy (s, str ); }};


samp input (){

char s [80];

samp str ;

cout << " Enter a string : ";

cin >> s;

str .set (s);

return str;}

int main (){

samp ob;

ob = input ();

ob. show ();

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 sp_greater(Car c, Truck t) {


return c.speed - t.speed;
}

int main() {
int t;
Car c1(6, 55), c2(2, 120);
Truck t1(10000, 55), t2(20000, 72);

cout << "Comparing c1 and t1:\n";


t = sp_greater(c1, t1);
if (t < 0)
cout << "Truck is faster.\n";
else if (t == 0)
cout << "Car and truck speed is the same.\n";
else
cout << "Car is faster.\n";

cout << "Comparing c2 and t2:\n";


t = sp_greater(c2, t2);
if (t < 0)
cout << "Truck is faster.\n";
else if (t == 0)
cout << "Car and truck speed is the same.\n";
else
cout << "Car is faster.\n";

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 isfactor(myclass ob) {


if (!(ob.n % ob.d))
return 1;
else
return 0;
}

int main() {

cout << "PRANTO MALAKER 409 \n";

myclass ob1(10, 2), ob2(13, 3);

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy