Function demo /* 1. Create a user defined function to find the sum of two numbers. 2. use/call the function in the main() 3. Function definition should be written after the function call */ #include<iostream> using namespace std; int main() { int sum(int , int ); // Function Declaration int num1 = 3, num2 = 3; // Local variables inside the main cout<<"Sum ="<<sum(num1,num2)<<endl; // Function Call return 0; } // Function definition int sum(int a, int b) { return(a+b); } /* 1. Create a user defined function to find the sum of two numbers. 2. use/call the function in the main() 3. Function definition should be written after the function call */ #include<iostream> using namespace std; int main() { int sum(int , int ); // Function Declaration int num1 = 3, num2 = 10; // Local variables inside the main cout<<"Sum ="<<sum(num1,num2)<<endl; // Function Call return 0; } // Function definition int sum(int a, int b) { int total = a+b; return(total); } /* 1. Create a user defined function to find the sum of two numbers. 2. use/call the function in the main() 3. Function definition should be written after the function call */ #include<iostream> using namespace std; int main() { int sum(int , int ); // Function Declaration int num1 = 3, num2 = 10; // Local variables inside the main int sumTotal = sum(num1,num2); cout<<"Sum ="<<sumTotal<<endl; // Function Call return 0; } // Function definition int sum(int a, int b) { int total = a+b; return(total); } /* 1. Create a user defined function to find the sum of two numbers. 2. use/call the function in the main() 3. Function definition is written before the function call */ #include<iostream> using namespace std; // Function definition int sum(int a, int b) { int total = a+b; return(total); } int main() { int num1 = 3, num2 = 10; // Local variables inside the main int sumTotal = sum(num1,num2); cout<<"Sum ="<<sumTotal<<endl; // Function Call return 0; } /* Demo of math functions (Library Functions) */ #include<iostream> #include<cmath> #include<iomanip> using namespace std; int main() { float f = 0.223; int i = 2; cout<<"Sine of the f = " <<sin(f)<<endl; cout<<"Sine of the i = " <<sin(i)<<endl; cout<<"**************************" <<endl; cout<<"Sine inverse of f = " <<asin(f)<<endl; cout<<"**************************" <<endl; cout<<"CoSine of the i = " <<cos(i)<<endl; cout<<"Cosine inverse of f = " <<acos(f)<<endl; cout<<"**************************" <<endl; cout<<"Tangent of the i = " <<tan(i)<<endl; cout<<"tangent inverse of f = " <<atan(f)<<endl; cout<<"**************************" <<endl; cout<<"Square root of 25"<<sqrt(25)<<endl; cout<<"Cube Square root of 8 = "<<cbrt(8)<<endl; cout<<"**************************" <<endl; cout<<" 2 to the power of 10 = "<<exp2(10)<<endl; cout<<"Hypotenuse of 2 and 4 = "<<hypot(2,4)<<endl; cout<<"**************************" <<endl; cout<<" 3 to the power of 5 = "<<pow(3,5)<<endl; cout<<"**************************" <<endl; cout<<floor(12.3)<<endl; cout<<ceil(12.3)<<endl; cout<<"**************************" <<endl; cout<<fabs(-12.3)<<endl; cout<<"**************************" <<endl; cout<<round(12.3)<<endl; cout<<fmax(12.3,33.3)<<endl; cout<<fmin(12.3,33.3)<<endl; cout<<"*******BASE Change************" <<endl; cout << setbase(16) << 3215<<endl; cout << setbase(8) << 3215<<endl; return 0; } /* Write a C++ program to do the following tasks. a)Create a user defined function to read a number and display whether it is odd or even b)Use the function in the main(). c)Call the function before the function definition.(Hint: Declare the function) */ #include<iostream> using namespace std; int main() { void checkOddEven(); // Function Declaration checkOddEven(); // Function Call return 0; } // Function Definition // Void Function and has no arguments void checkOddEven() { int x; cout<<"Enter the value of x"<<endl; cin>>x; if (x%2==0) cout<<" Even Number "<<endl; else cout<<" Odd Number "<<endl; } /* Write a C++ program to do the following tasks. a)Create a user defined function to read a number as an argument and display whether it is odd or even b)Use the function in the main(). c)Call the function before the function definition.(Hint: Declare the function) */ #include<iostream> using namespace std; int main() { void checkOddEven(int x); // Function Declaration int num; cout<<"Enter the number"<<endl; cin>>num; checkOddEven(num); // Function Call return 0; } // Function Definition // Void Function with arguments void checkOddEven(int x) { if (x%2==0) cout<<" Even Number "<<endl; else cout<<" Odd Number "<<endl; } /* Write a C++ program to do the following tasks. a) Create a user defined function with parameter to read a number check whether it is odd or even b) Pass the number to check as an argument c) The function should return 0 if the number is even else return 1 d) Use the function in the main() to display whether the number is odd or even e) Call the function before the function definition unction before the function definition.(Hint: Declare the function) */ #include<iostream> using namespace std; int main() { int oddOrEven(int x); // Function Declaration int num; cout<<"Enter the number"<<endl; cin>>num; int result = oddOrEven(num); if (result == 0) cout<<"The number is even"<<endl; else cout<<"The number is odd"<<endl; return 0; } // Function Definition // Void Function with arguments int oddOrEven(int a) { if(a%2==0) return 0; else return 1; } /* Write a C++ program to do the following tasks. a) Create a user defined function with parameter to read a number check whether it is odd or even b) Pass the number to check as an argument c) The function should return 0 if the number is even else return 1 d) Use the function in the main() to display whether the number is odd or even e) Call the function before the function definition unction before the function definition.(Hint: Declare the function) */ #include<iostream> using namespace std; int main() { int oddOrEven(int x); // Function Declaration int num; cout<<"Enter the number"<<endl; cin>>num; /* int result = oddOrEven(num); if (result == 0) cout<<"The number is even"<<endl; else cout<<"The number is odd"<<endl; */ if (oddOrEven(num)== 0) cout<<"The number is even"<<endl; else cout<<"The number is odd"<<endl; return 0; } // Function Definition // Void Function with arguments int oddOrEven(int a) { if(a%2==0) return 0; else return 1; } /* Write a C++ program to do the following tasks. a) Create a user defined function to read and display your first name only (without any spaces) b) Use the function in the main() to read and display your name. c) Call the function after the function definition. */ #include<iostream> using namespace std; // Function definition void displayName() { char name[20]; cout<<"Enter your first name only"<<endl; cin>>name; cout<<"Your name is " <<name<<endl; } int main() { displayName(); return 0; } /* Write a C++ program to do the following tasks. a) Create a user defined function to read and display your full name only (name with spaces ; eg: Amar Anthony John ) b) Use the function in the main() to read and display your name. c) Call the function after the function definition. */ #include<iostream> using namespace std; // Function definition void displayName() { char name[20]; cout<<"Enter your full name only"<<endl; //cin.getline(name,20); gets(name); cout<<"Your first name is " <<name<<endl; } int main() { displayName(); return 0; } /* 1. Write a C++ program to do the following tasks a. Define independent functions to compute and return the sum(a+b), difference(a-b) , product(a*b) and quotient (a/b) b. Validate the function definitions with real arguments c. Use these function definitions to compute the expression ((3 + (15/3) * 8)10) */ #include <iostream> #include <cmath> #include <iomanip> using namespace std; int findSum(int a, int b) { return a+b; } int findDiff(int a, int b) { return a-b; } int findProduct(int a , int b) { return a*b; } double findQuot(double a , double b) { return a/b; } main() { cout<<"Find Sum of 2 and 3 = "<<setw(5)<<findSum(2,3)<<endl; cout<<"Find Difference of 12 and 3 = "<<setw(5)<<findDiff(12,3)<<endl; cout<<"Find Product of 4 and 3 = "<<setw(5)<<findProduct(4,3)<<endl; cout<<"Find Quotient of 12 and 3 = "<<setw(5)<<findQuot(12,3)<<endl; // Use the functions defined above to compute the expression ((3 + (15/3) * 8)-10) double ans = findDiff(findSum(findProduct(findQuot(15,3),8),3),10); cout<<" Computed value of the expression ="<<setw(5)<<ans<<endl; return 0; } Array /* Declare and initialize a One Dimensional integer Array to store your 5 Quiz marks Writing a function/ Define a function to display the elements in the array. Pass the array as an argument. Use this function in the main() */ #include<iostream> #include<iomanip> using namespace std; void displayArray(int a[5]) { cout<<"Elements of the array is " <<endl; for(int i=0;i<5;i++) {cout<<setw(5)<<a[i]<<endl;} } int main() { int quiz[5] = {12,24,32,26,25}; displayArray(quiz); return 0; } /* Declare and initialize a One Dimensional integer Array to store your 5 Quiz marks Writing a function/ Define a function to display the square of the elements in the array. Define a function to return/find/computer the average marks of the quiz Pass the array as an argument. Use this function in the main() */ #include<iostream> #include<iomanip> #include<cmath> using namespace std; void displayArray(int a[5]) { cout<<"Elements of the array is " <<endl; for(int i=0;i<5;i++) {cout<<setw(5)<<pow(a[i],2)<<endl;} } double findAverage(int n[5]) { int sum(0); // Local Variable sum for(int i=0;i<5;i++) { sum = sum + n[i]; } return (sum/5.0); } int main() { int quiz[5] = {12,24,32,26,25}; displayArray(quiz); cout<<"Average Marks = "<<setw(5)<<findAverage(quiz)<<endl; return 0; } /* Passing 2D array as argument to a function Declare two 2D integer array of size 2,3 (2 rows and 3 cos) and initialize it. Write a function sumArray() to add the corresponding elements of both the arrays and displays it Call the function in the main() to display the results */ #include<iostream> #include<iomanip> #include<cmath> using namespace std; int main() { int match[2][3] = { {1,2,3}, {4,5,6} }; return 0; } /* Passing 2D array as argument to a function Declare two 2D integer array of size 2,3 (2 rows and 3 cos) and initialize it. Write a function sumArray() to add the corresponding elements of both the arrays and displays it Call the function in the main() to display the results */ #include<iostream> #include<iomanip> using namespace std; void arraySum(int a[][3],int b[][3]) { int temp[2][3]; for(int i(0); i<2; i++) {for(int j(0); j<3; j++) {temp[i][j] = a[i][j]+b[i][j]; cout<<setw(5)<<temp[i][j]<<endl; } } } int main() { int match1[2][3] = { {1,2,3}, {4,5,6} }; int match2[2][3] = { {11,21,31}, {41,51,61} }; arraySum(match1,match2); return 0; } (nesting function) /* Write function definitions each to compute the sum eg(a+b), difference (eg,a-b), product(eg a*b) quotient (eg a/b) . Validate these functions in main() using some real arguments Use the functions defined above to compute the expression ((3 + (15/3) * 8)-10) */ #include<iostream> #include<iomanip> using namespace std; int findSum(int a, int b) { return a+b; } int findDiff(int a, int b) { return a-b; } int findProd(int a, int b) { return a*b; } double findQuot(double a, double b) { return a/b; } int main() { cout<<"Find Sum of 2 and 3 = "<<setw(5)<<findSum(2,3)<<endl; cout<<"Find Difference of 12 and 3 = "<<setw(5)<<findDiff(12,3)<<endl; cout<<"Find Product of 4 and 3 = "<<setw(5)<<findProd(4,3)<<endl; cout<<"Find Quotient of 12 and 3 = "<<setw(5)<<findQuot(12,3)<<endl; // Use the functions defined above to compute the expression // ((3 + (15/3) * 8)-10) double ans = findDiff(findSum(findProd(findQuot(15,3),8),3),10); cout<<endl; cout<<endl; cout<<"Answer = "<<setw(10)<<ans<<endl; return 0; } (char array) // Char Array functions #include<iostream> #include<cstring> using namespace std; int main() { char name1[20] = "Abdul"; char name2[20] = "Aziz"; cout<<"First Name = "<<name1<<endl; cout<<"Second Name = "<<name2<<endl; cout<<"Length of the char string = " <<strlen(name1)<<endl; cout<<"Full Name = "<<strcat(name1,name2)<<endl; } ( Function overloadig ) /* Write overloaded function definitions to find the maximum of three integers/ three double/ three characters Validate the functions in the main */ #include<iostream> using namespace std; int findMax(int x, int y, int z) { return (((x>y)?x:y)>z)?((x>y)?x:y):z; /*if((x>y)&&(x>z)) return x; else if (((y>x)&&(y>z))) return y; else return z; */ } double findMax(double x, double y, double z) { if((x>y)&&(x>z)) return x; else if (((y>x)&&(y>z))) return y; else return z; } char findMax(char x, char y, char z) { if((x>y)&&(x>z)) return x; else if (((y>x)&&(y>z))) return y; else return z; } int main() { cout<<" The maximum of 12, 34, 43 =" <<findMax(12,34,43)<<endl; cout<<" The maximum of 1.2, 142.4, 4.3 =" <<findMax(1.2,142.4,4.3)<<endl; cout<<" The maximum of 'A', 'x','d' =" <<findMax('A', 'x','d')<<endl; return 0; } /* Write overloaded function definitions to find the maximum of three integers/ three double/ three characters Validate the functions in the main */ #include<iostream> using namespace std; int findMax(int x, int y, int z) { return (((x>y)?x:y)>z)?((x>y)?x:y):z; /*if((x>y)&&(x>z)) return x; else if (((y>x)&&(y>z))) return y; else return z; */ } double findMax(double x, double y, double z) { return (((x>y)?x:y)>z)?((x>y)?x:y):z; } char findMax(char x, char y, char z) { return (((x>y)?x:y)>z)?((x>y)?x:y):z; } int main() { cout<<" The maximum of 12, 34, 43 =" <<findMax(12,34,43)<<endl; cout<<" The maximum of 1.2, 142.4, 4.3 =" <<findMax(1.2,142.4,4.3)<<endl; cout<<" The maximum of 'A', 'x','d' =" <<findMax('A', 'x','d')<<endl; return 0; } /* Write overloaded function definitions to find the minimum of two integers/ two double/ two characters Validate the functions in the main */ #include<iostream> using namespace std; int findMin(int x, int y) { return ( (x<y)?x:y); } double findMin(double x, double y) { return ( (x<y)?x:y); } char findMin(char x, char y) { return ( (x<y)?x:y); } int main() { cout<<" The maximum of 12, 34 =" <<findMin(12,34)<<endl; cout<<" The maximum of 1.2, 142.4 =" <<findMin(1.2,142.4)<<endl; cout<<" The maximum of 'A', 'x'=" <<findMin('A', 'x')<<endl; return 0; } ( FunctionTemplate ) /* Analyze the overloaded function definitions given in the program. Rewrite the program using a function template */ #include<iostream> using namespace std; /* int findMin(int x, int y) { return ( (x<y)?x:y); } double findMin(double x, double y) { return ( (x<y)?x:y); } char findMin(char x, char y) { return ( (x<y)?x:y); } */ template <class T> T findMin(T a,T b) { return(a<b)?a:b; } int main() { cout<<" The minimum of 12, 34 =" <<findMin(12,34)<<endl; cout<<" The minimum of 1.2, 142.4 =" <<findMin(1.2,142.4)<<endl; cout<<" The minimum of 'A', 'x'=" <<findMin('A', 'x')<<endl; return 0; } /* Define a function template the find the sum of two integers/ two double variables. Validate/Use this function in the main. */ #include<iostream> using namespace std; // Function Template template <class B> B findSum(B a,B b) { return(a+b); } int main() { int a, b; double x,y; cout<<"Enter two integer values"<<endl; cin>>a>>b; cout<<"The Sum of a and b = "<<findSum(a,b)<<endl; cout<<"Enter two double values"<<endl; cin>>x>>y; cout<<"The Sum of x and y = "<<findSum(x,y)<<endl; return 0; } /* Define a function template the find the sum of two integers/ two double variables. Validate/Use this function in the main. */ #include<iostream> using namespace std; // Function Template template <class B> B findSum(B a,B b) { return(a+b); } int main() { int a, b; double x,y; cout<<"Enter two integer values"<<endl; cin>>a>>b; cout<<"The Sum of a and b = "<<findSum<int>(a,b)<<endl; cout<<"Enter two double values"<<endl; cin>>x>>y; cout<<"The Sum of x and y = "<<findSum<double>(x,y)<<endl; return 0; } /* Define a function template the find the sum of two integers/ two double variables. Validate/Use this function in the main. */ #include<iostream> using namespace std; // Function Template template <typename B> B findSum(B a,B b) { return(a+b); } int main() { int a, b; double x,y; cout<<"Enter two integer values"<<endl; cin>>a>>b; cout<<"The Sum of a and b = "<<findSum<int>(a,b)<<endl; cout<<"Enter two double values"<<endl; cin>>x>>y; cout<<"The Sum of x and y = "<<findSum<double>(x,y)<<endl; return 0; } (Data type Modifiers) /* Datatype Modifiers/ Datatype Specifiers in C++ signed and unsigned short and long */ #include<iostream> using namespace std; int main() { // int i(-12) ; by default it is signed signed int i(-12); cout<<"i = "<< i<<endl; // Declare an unsigned integer - which takes only +ive values //unsigned int ui(120); unsigned ui(120); cout<<"ui = "<< ui<<endl; // Try giving negative value to ui and observe the output ; output is a junk value //short int shti(12); short shti(12); cout<<"size of the short integer shti = "<<sizeof(shti)<<"bytes"<<endl; //long int li(23); long li(23); cout<<"size of the long integer li = "<<sizeof(li)<<"bytes"<<endl; long long lli(23); cout<<"size of the long long integer lli = "<<sizeof(li)<<"bytes"<<endl; return 0; } (Data type Qualifiers) /* Data type qualifiers : Volatile and Constant (const) */ #include<iostream> #include<cmath> using namespace std; int main() { // Declare and initialize a volatile variable int x(10); cout<<"x = "<<x<<endl; x += 2; cout<<"x = "<<x<<endl; cout<<"Enter a new value to x"<<endl; cin>>x; cout<<"x = "<<x<<endl; // Declare a read only (const) variable const double pi(3.1456); cout<<" Area of a circle radius 2.3 = "<<(pi*exp2(2.3))<<endl; // Try changing the value of the read only variable pi and see the error pi = pi + 0.02; return 0; } ( namespace) /* Namespaces in C++ A namespace is designed to overcome the difficulty to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope */ #include<iostream> using namespace std; namespace mySpace { int x(100); } // first name space namespace first_space { void func() { cout << "Inside first_space" << endl; } } // second name space namespace second_space { void func() { cout << "Inside second_space" << endl; } } int main() { int x(10); // This variable is in the standard name space std::cout<<"x in std namespace = "<<x<<std::endl; // print x in namespace mySpace cout<<"x in the namespace mySpace= "<<mySpace::x<<endl; // Calls function from first name space. first_space::func(); // Calls function from second name space. second_space::func(); return 0; } (Qualifiers) /* Data type qualifiers : Volatile and Constant (const) */ #include<iostream> #include<cmath> using namespace std; int main() { // Declare and initialize a volatile variable int x(10); cout<<"x = "<<x<<endl; x += 2; cout<<"x = "<<x<<endl; cout<<"Enter a new value to x"<<endl; cin>>x; cout<<"x = "<<x<<endl; // Declare a read only (const) variable const double pi(3.1456); //const double pi; cout<<"Value of pi = "<<pi<<endl; // Try changing the value of the read only variable pi and see the error // pi = pi + 0.02; cout<<" Area of a circle radius 2.3 = "<<(pi*exp2(2.3))<<endl; return 0; } (Size of operator) /* sizeof() : operator to find the storage size of objects in C++ */ #include<iostream> using namespace std; int main() { int i(10); cout<< "\t"<<" i = " << "\t"<< i << endl; cout<< "\t"<<" Size of int i = " << "\t"<< sizeof(i) << " bytes "<<endl; float f(12.3); cout<< "\t"<<" f = " << "\t"<< f << endl; cout<< "\t"<<" Size of float f = " << "\t"<< sizeof(f) << " bytes "<<endl; cout<<endl; double d(123.345); cout<< "\t"<<" d= " << "\t"<< d << endl; cout<< "\t"<<" Size of double d = " << "\t"<< sizeof(d) << " bytes "<<endl; cout<<endl; cout<<endl; cout<< "\t"<<" Size of a void = " << "\t"<< sizeof(void) << " bytes "<<endl; cout<<endl; cout<< "\t"<<" Size of a boolean variable= " << "\t"<< sizeof(bool) << " bytes "<<endl; cout<<endl; cout<< "\t"<<" Size of a char variable= " << "\t"<< sizeof(char) << " bytes "<<endl; return 0; } (STRINGFUNCTIONS) /* Storage/representing strings in C++ using the objects of string class <string> */ #include<iostream> #include<string> using namespace std; int main() { string str1 = "muscat"; string str2 = "dublincity"; string str3; // Print the contends of the strings: cout<<" Str 1="<<"\t"<<str1<<endl; cout<<" Str 2="<<"\t"<<str2<<endl; //Find the lengths of str1 and str2 cout<<"\tLength of Str1 "<<"\t"<<str1<<"\t = "<<str1.size()<<endl; cout<<"\tLength of Str2 "<<"\t"<<str2<<"\t = "<<str2.length()<<endl; //Find the sum of lengths of Str1 and str2 cout<<"\t sum of lengths of Str1 and Str2 "<<"\t"<<(str1.size()+str2.size())<<endl; // Copy str2 to str 3 str3 = str2; cout<<" Str 3="<<"\t"<<str3<<endl; //Concatenate str1 and str2 cout<<endl; str1 = str1 + str2 ; cout<<"Str1 after concatenation with str 2 = "<<"\t"<<str1<<endl; return 0; } (Stringi supper lowe digit) /* 3. Write a C++ code do the following. a. Define a char variable str1 and assign "fgkjbDd lsdfk9834$2$%^WWf'dj fgkj&ljds^# hjr WG". b. Print the number of characters c. Print the number of Upper case letters d. Print the number of Lower case letters e. Print the number of digits f. Print the number of blank spaces. g. Print the number of special characters. */ #include <iostream> #include <cstring> using namespace std; int main() { char str1[100] = "fgkjbDd lsdfk9834$2$%^WWf'dj fgkj&ljds^# hjr WG"; int i, len=0, uc=0, lc=0, dg=0, bl=0, sp=0, d=0; for(i=0; i<strlen(str1) ; i++) { if(isupper(str1[i])) uc++; else if(islower(str1[i])) lc++; else if(isdigit(str1[i])) dg++; else if(isblank(str1[i])) bl++; else if(str1[i] == 'd') d++; else sp++; } cout cout cout cout << << << << "Given String : "String Length : "No of Upper case Chars : "No of Lower case Chars : " " " " << << << << str1 << endl; strlen(str1) << endl; uc << endl; lc << endl; cout cout cout cout << << << << "No "No "No "No of of of of digits blank spaces spl chars. 'd' chars. : : : : " " " " << << << << dg << endl; bl << endl; sp << endl; d << endl; } (defineandundefinemacro) /* PREDEFINED MACROS */ // Null directive # // Telling the compiler to include the file iostream.h from the include folder #include<iostream> #include<cmath> using namespace std; int main() { double radius; cout<<"This is line no: " <<__LINE__<<endl; cout<<"Enter the radius" << endl; cin>>radius; cout<<"Area of the circle = " <<3.1456*pow(radius,2)<<endl; cout<<"Circumference of the circle = " <<2 * 3.1456* radius<<endl; cout<<endl; cout<<"This is line no: " <<__LINE__+2<<endl; cout<<"File Name " <<__FILE__<<endl; cout<<"Date of Compilation " <<__DATE__<<endl; cout<<"Time of Compilation " <<__TIME__<<endl; return 0; } (includemacro) /* PREPROCESSOR STATEMENT COMPILER DIRECTIVES */ // Null directive # // Telling the compiler to include the file iostream.h from the include folder #include<iostream> #include<cmath> // Define a macro with the replacement text 3.1456 #define PI 3.1456 using namespace std; int main() { // Find the area of a circle pi * (radius)2 double radius; cout<<"Enter the radius" << endl; cin>>radius; cout<<"Area of the circle" <<PI*pow(radius,2)<<endl; return 0; } (NameSpaceDemo) /* Namespaces in C++ A namespace is designed to overcome the difficulty to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope */ #include<iostream> using namespace std; namespace mySpace { int x(100); // This variable is in the mySpace name space double ff(12.3); // This variable is in the mySpace name space } // first name space namespace first_space { void foo() { cout << " I am Inside the first_space namespace" << endl; } } // second name space namespace second_space { void foo() { cout << "I am Inside the second_space namespace" << endl; } } int main() { int x(10); // This variable is in the standard name space std::cout<<"x in std namespace = "<<x<<std::endl; // Print the values of the variables in the mySpace namespace cout<<"x in mySpace namespace = "<<mySpace::x<<endl; cout<<"ff in mySpace namespace = "<<mySpace::ff<<endl; // call foo() inside first_space first_space::foo(); // call foo() inside second_space second_space::foo(); return 0; } (typedef) /* Typedefin C++ tydefening your own datatypes giving a new name (aliasing) to the existing datatype */ #include<iostream> int main() { typedef int cars; typedef double battle; battle balance(12.3); cars x(12); std::cout<<"x = " <<x<< std::endl; std::cout<<"balance = " <<balance<< std::endl; return 0; }