0% found this document useful (0 votes)
23 views

Lecture 3

Lab Report

Uploaded by

md emam hossain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture 3

Lab Report

Uploaded by

md emam hossain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 3

Const and enum expression


Default Argument in Function
Inline Function
User Defined Function
Function Overloading

1
Symbolic Constants:
There are two ways of symbolic constants in C++:
 Using const
 Using enum

#include <iostream>
using namespace std;

const double pi = 3.1416;


const char newline = '\n';

C++ program using int main ()


const {
double r=5.0;
double circle;

circle = 2 * pi * r;
cout << circle;
cout << newline;
}
enum operator:
An enumeration provides context to describe a range of values which are
represented as named constants and are also called enumerators.

#include <iostream>
using namespace std;

enum seasons { spring, summer, autumn, winter };

int main() {

cout<<"\nC++ program using enum operator\n";

cout<<spring<<endl<<summer<<endl<<autumn<<endl<<win
ter;
return 0;
}
enum
#include <iostream>
using namespace std;

enum week { Sunday, Monday, Tuesday,


Wednesday, Thursday, Friday, Saturday };

int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}
Switch using enum
C++ Program to demonstrate working of default argument
#include <iostream> cout<<"\n\nBoth argument passed:\
using namespace std; n";
display('$', 5);
void display(char c= '*', int n= 3);
return 0;
int main() { }
cout<<"No argument passed:\n";
display(); void display(char c, int n){
for(int i = 1; i <=n; i++) {
cout<<"\n\nFirst argument cout<<c;
passed:\n"; }
display('#'); cout<<endl;
}
Example using iomanip:
Manipulators are operators that are used to format the data format the data display.
The most commonly used manipulators are endl and setw.
Simple example using iomanip
#include <iostream>
#include <iomanip>
using namespace std;
main ()
{
float a,b,c;
a = 7;
b = 3;
c = a/b;
cout << setprecision (1) << c << endl;
cout << setprecision (2) << c << endl;
cout <<setprecision (3) << c << endl;
cout << setprecision (4) << c << endl;
cout << setprecision (5) << c << endl;
cout << setprecision (6) << c << endl;
}
Control Structure
Three types of control structures:

Basic Control Structure


Inline Function
 C++ inline function is powerful concept that is commonly used with classes.
 If a function is inline, the compiler places a copy of the code of that function
at each point where the function is called at compile time.

When inline functions are used, the overhead of function call is eliminated.
Instead, the executable statements of the function are copied at the place of each
function call. This is done by the compiler

A function definition in a class definition is an inline function definition,


even without the use of the inline specifier.
Program using Inline Function
#include <iostream>
using namespace std;

inline int Max(int x, int y)


{
return (x > y)? x : y;
}
// Main function for the program
int main( )
{
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}
Program using Inline Function
User-defined Functions

User-defined functions in C++


· Value-returning functions - functions that have a data type.
· Void functions - functions that do not have a data type.
Void functions
#include<iostream>
using namespace std;

void MyPrint()
{
cout << "Printing from a
function.\n";
}

int main()
{
MyPrint();
return 0;
}
Value-returning functions
#include<iostream>
using namespace std;
int Add(int output1, int output2)
{
return output1 + output2;
}
int main()
{
int answer, input1, input2;
cout << "Give a integer number:";
cin >> input1;
cout << "Give another integer number:";
cin >> input2;
answer = Add(input1,input2);
cout << input1 << " + " << input2 << " =
" << answer;
return 0;
}
Function to return the larger of two numbers.

double larger(double x, double y)


{
double max;

if(x >= y)
max = x;
else
max = y;
return max;
}
Write a program using function which accept two integers as an argument and
return its sum.
# include <iostream>
using namespace std;
int sum(int, int);
int main()
{ int x,y,s;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
s=sum(x,y);
cout<<"The sum is : "<<s;
return 0;
}
int sum(int a, int b)
{ int total;
total=a+b;
return total;
}
Write a function to calculate the factorial value of any integer as an argument.

#include<iostream>
using namespace std;
int factorial(int);
int main(){
int x,f;
cout<<"Enter number : ";
cin>>x;
f=factorial(x);
cout<<"The factorial is :"<<f;
return 0;}
int factorial(int a){
int fact=1;
while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}
Categories of User Defined Function

For better understanding of arguments and return in functions,


user-defined functions can be categorized as:

 Function with no argument and no return value.

 Function with no argument but return value.

 Function with argument but no return value.

 Function with argument and return value.


Function Overloading:
#include <iostream> void test(int var)
using namespace std; {
void test(int); cout<<"Integer number:
void test(float); "<<var<<endl;
void test(int, float); }
int main() { void test(float var)
int a = 10; {
float b = 50.5; cout<<"Float number:
test(a); "<<var<<endl;
test(b); }
test(a, b); void test(int var1, float var2)
return 0; {
} cout<<"Integer number:
"<<var1;
cout<<" And float
number:"<<var2<<endl;
Function Overloading:
In C++ programming, two functions can have same identifier(name) if either number of arguments or type of
arguments passed to functions are different.

These types of functions having similar name are called overloaded functions.

• Two or more overloaded functions can have the same name


• However, either the argument types or the number of the arguments or both of the overloaded functions
must different.
• Return type of the overloaded functions might be the same or different
• Return type alone is not a sufficient difference to allow function overloading
• Reduces the complexity of a program by allowing related operations to be referred to by the same name

/* Example of function overloading */


int test() { }
int test(int a){ }
int test(double a){ }
int test(int a, double b){ }
#include <iostream>
using namespace std;
void Sum(int a, int b);
void Sum(double a, int b);

int main()
{
Sum(10, 5);
Sum(10.5, 5);
return 0;
}
void Sum(int a, int b)
{
cout<< "Integer value: " << a+b <<endl ;
}
void Sum(double a, int b)
{
cout<< "double value: " << a+b <<endl ;
}
void FunctionOverloading::Sum(int a, int b)
#include <iostream>
{
using namespace std;
cout<< "Integer value: " << a-b<<endl ;
}
class FunctionOverloading
int main()
{
{
public: FunctionOverloading ob;
void Sum(int a, int b); ob.Sum(25,10);
void Sum(double a, int b); ob.Sum(25.5,5);
}; return 0;
void FunctionOverloading::Sum(double a, int b) }
{
cout<<"Double Value: " <<a+b<<endl;
}

You might also like