Lecture 3
Lecture 3
1
Symbolic Constants:
There are two ways of symbolic constants in C++:
Using const
Using enum
#include <iostream>
using namespace std;
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;
int main() {
cout<<spring<<endl<<summer<<endl<<autumn<<endl<<win
ter;
return 0;
}
enum
#include <iostream>
using namespace std;
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:
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
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.
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
These types of functions having similar name are called overloaded functions.
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;
}