Problem 1: Using CPP code show the use of If…Then…Else statement-using user input values.
Code
//
// main.cpp
// Hello World CPP
//
// Created by Bishal
Ghimire on 9/19/13.
// Copyright (c) 2013
Bishal Ghimire. All rights reserved.
//
#include <iostream>
using namespace std;
/**
IF then else function
for using user input values
*/
int main(int argc, const char * argv[]) {
int inputAge;
std::cout << "Input
You Age, !\n";
cin >>
inputAge;
if (inputAge
>= 18) {
cout << "You
can Vote ! \n";
}
else if (inputAge
< 18) {
cout << "You
can NOT vote ! \n";
}
return 0;
}
Output
$ g++ main.cpp
$ ./a.out
Input You Age, !
29
You can Vote !
Problem 2: Using functions show how you can swipe variables using call by reference.
Code
/**
Function to swipe two
variables using third temp value
*/
void swap(int *u, int *v) {
int temp;
temp = *u;
*u = *v;
*v = temp;
return;
}
int main(int argc, const char * argv[]) {
int firstValue;
cout<<"Input
First Value \n";
cin >>
firstValue;
int secondValue;
cout<<"Input
Second Value \n";
cin >>
secondValue;
swap(firstValue,
secondValue);
cout<<"First
Value and Second Value after they are Swaped \n";
cout << "1st =
" << firstValue << "\n2nd = " <<
secondValue;
cout<<"\n";
return 0;
}
Output
$ g++ main.cpp
$ ./a.out
Input First Value
12
Input Second Value
34
First Value and Second Value after they are Swaped
1st = 34
2nd = 12
Problem 3: Demonstrate the use of functional overloading in CPP (C++)
Code
/**
functionOverLoading.cpp
*/
#include "functionOverLoading.h"
using namespace std;
#define pi 3.141
/**
Function overloading
*/
class fxn {
public:
void area(int); // circle
void area(int, int); // rectangle
void area(int, int, int); // triangle
};
void fxn::area(int a) {
cout<<"Area
of Circle: "<<pi*a*a;
cout<<"\n";
}
void fxn::area(int a, int b) {
cout<<"Area
of rectangle: "<<a*b;
cout<<"\n";
}
void fxn::area(int a, int b, int c) {
// cout<<"Area
of triangle: "<<a*b*c;
cout<<"\n";
}
typedef enum ChoiceType
{
choiceCircle = 1,
choiceRectangle,
choiceTriangle,
choiceExit
} ChoiceType;
int main(int argc, const char * argv[]) {
int choice;
int radius;
int length;
int breath;
int sideA, sideB,
sideC;
fxn object;
cout<<"\n\t\tFunction
Overloading";
cout<<"\n 1.
Area of Circle \n 2. Area of Rectangle \n 3. Area of Triangle \n 4. Exit \n
";
cout<<"Enter
your Choice:";
cin>>choice;
switch (choice) {
case choiceCircle:
cout<<"Enter
Radious of the Circle:";
cin>>radius;
object.area(radius);
break;
case choiceRectangle:
cout<<"Enter
Sides of Rectangle:";
cin>>length>>breath;
object.area(length,
breath);
break;
case choiceTriangle:
cout<<"Enter
Sides of triangle:";
cin>>sideA>>sideB>>sideC;
object.area(sideA, sideB,
sideC);
break;
case choiceExit:
exit(0);
break;
default:
break;
}
return 0;
}
Output
$g++ functionOverLoading.cpp –o functionOverLoading
$ ./functionOverLoading
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter your Choice:1
Enter Radious of the Circle:10
Area of Circle: 314.1
$ ./functionOverLoading
Function
Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter your Choice:2
Enter Sides of Rectangle:10 12
Area of rectangle: 120
Congratulation for the great post. Those who come to read your Information will find lots of helpful and informative tips. IOS Developer
ReplyDelete