Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to C++ Lecture No. 27

2,193 views

Published on

Namespaces in C++
Unnamed Namespace in C++
The Using Declaration in C++
Nested Namespaces in C++
Inline Namespaces in C++
Preprocessor in C++
Type of Preprocessor in C++
Function-Like Macros in C++
Conditional Compilation in C++
The # and ## Operators in C++
Predefined C++ Macros

Published in: Education
  • Can you please share your C++ ppt's on my email ID anjaneya.ugi@gmail.com it will surely be helpful Thanks in advance
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Introduction to C++ Lecture No. 27

  1. 1. Introduction to C++ Basic and Advanced Concepts Lecture No. 27 My Email Address adilaslam5959@gmail.com Introduction to C++ Lecture Slides By Adil Aslam
  2. 2. About Me Introduction to C++ Lecture Slides By Adil Aslam i am Student of BSCS My email address: adilaslam5959@gmail.com
  3. 3. Introduction to C++ Introduction to C++ Lecture Slides By Adil Aslam
  4. 4. Course Contents • Variables in C++ • Data Types in C++ • Operators in C++ • sizeof and typedef in C++ • Selection / Multiple Selection in C++ • Iterations/Loops in C++ • Built-in Functions in C++ • User-defined Functions in C++ • Inline Functions in C++ • Function Overloading in C++ Introduction to C++ Lecture Slide By Adil Aslam
  5. 5. Course Contents • Recursion in C++ • Type of Recursion • Arrays in C++ • Two Dimensional Arrays • Three Dimensional Arrays • Pointers in C++ • Class String in C++ • Dynamic Memory Allocation in C++ • Classes in C++ • Constructor/Destructor in C++ • Storage Classes in C++ Introduction to C++ Lecture Slides By Adil Aslam
  6. 6. Course Contents • Inheritance in C++ • Type of Inheritance • Polymorphism in C++ • Type of Polymorphism • Operator Overloading in C++ • Encapsulation in C++ • Friend Functions in C++ • Friend Classes in C++ • Virtual Functions in C++ • Pure Virtual Functions in C++ Introduction to C++ Lecture Slides By Adil Aslam
  7. 7. Course Contents • Type Conversion in C++ • Template in C++ • Exception Handling in C++ • File Handling in C++ • Explicit and Mutable keywords in C++ • Preprocessor Directive in C++ • Manipulating Strings in Detail • Namespace in C++ • Standard Template Library in C++ • Some New Features in C++ Introduction to C++ Lecture Slides By Adil Aslam
  8. 8. Importance of this Course • Core of Computer Science • Many courses depend on this course • Object Oriented Programming • Data Structures • Advance Programming Techniques • Computer Graphics • ……. Introduction to C++ Lecture Slides By Adil Aslam
  9. 9. Text Book • Book Name: • C++ How to Program, 8th Edition • Author: • Paul Deitel, Harvey Deitel • Publishers: • Prentice Hall Introduction to C++ Lecture Slides By Adil Aslam
  10. 10. Text Book • Book Name: • C++: The Complete Reference, 4th Edition • Author: • Herbert Schildt • Publishers: • McGraw-Hill Education Introduction to C++ Lecture Slides By Adil Aslam
  11. 11. In This Lecture We Learn • Namespaces in C++ • Unnamed Namespace in C++ • The Using Declaration in C++ • Nested Namespaces in C++ • Preprocessor in C++ • Function-Like Macros in C++ • Conditional Compilation in C++ • The # and ## Operators in C++ • Predefined C++ Macros Introduction to C++ Lecture Slides By Adil Aslam
  12. 12. Introduction to C++ Lecture Slides By Adil Aslam Namespaces in C++
  13. 13. Namespaces in C++ • Consider a situation, when we have two persons with the same name, Adil, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area if they live in different area or their mother or father name, etc. • Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code. Introduction to C++ Lecture Slides By Adil Aslam
  14. 14. Namespaces in C++ • Namespace is used to define a scope where identifiers like variables, functions, classes, etc. are declared. The main purpose of using a namespace is to prevent ambiguity that may occur when two identifiers have same name. • For example, Run is an English word which can have different meanings that vary according to the context, like His running won him a gold medal at the Olympics and He's running a movie theater. Here, meaning of the some additional information/context is provided to prevent the ambiguity. Introduction to C++ Lecture Slides By Adil Aslam
  15. 15. Namespaces in C++ • Likewise, in C++, if there are two same named functions say xyz() performing different tasks. In order to prevent ambiguity while calling one of the two functions, we can declare them in two separate namespaces. Suppose, one of these function is declared in namespace name1and another is declared in name2. Then we can call them separately as name1::xyz() and name2::xyz(). • C++ uses a standard namespace called std where all the built-in classes and functions are declared. So, we use the statement using namespace std; at the beginning of our C++ program to use members defined in namespace std. Introduction to C++ Lecture Slides By Adil Aslam
  16. 16. Namespaces in C++ • A namespace is designed to overcome this difficulty and is used as additional information 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. • Namespace is a container for identifiers. It puts the names of its members in a distinct space so that they don't conflict with the names in other namespaces or global namespace. Introduction to C++ Lecture Slides By Adil Aslam
  17. 17. Namespaces in C++ • Using namespace we can have class, global variables, functions under one name. We can change global scope to Sub-scope. Introduction to C++ Lecture Slides By Adil Aslam
  18. 18. Summary • Namespace is a logical compartment used to avoid naming collisions. • The naming conflict or Collision always Occurs may occur: • When same program is using more than one library with some variables or functions having same name. • The name collision may occur for global variables, global functions, classes etc. • Default namespace is global namespace and can access global data and functions by proceeding (::) operator. • We can create our own namespace. And anything declared within namespace has scope limited to namespace. Introduction to C++ Lecture Slides By Adil Aslam
  19. 19. Problem Without Namespace // A Program to Demonstrate Need of Namespace #include <iostream> using namespace std; int main() { int value; value = 0; double value; // Compiler Error here value = 0.0; } Introduction to C++ Lecture Slides By Adil Aslam Compiler Error [Error] conflicting declaration 'double value‘ [Note] previous declaration as 'int value'
  20. 20. Problem Without Namespace // A Program to Demonstrate Need of Namespace #include <iostream> using namespace std; int main() { int value; value = 0; double value; // Compiler Error here value = 0.0; } Introduction to C++ Lecture Slides By Adil Aslam Compiler Error [Error] conflicting declaration 'double value‘ [Note] previous declaration as 'int value' In each Scope, a Name can only Represent one entity. So, there cannot be two Variables with the Same Name in the Same Scope. Using Namespaces, we can Create two Variables or Member Functions having the Same Name.
  21. 21. Use of Namespace #include <iostream> using namespace std; // Variable Created Inside Namespace namespace first { int val = 500; } // Global Variable int val = 100; int main() { // Local Variable int val = 200; // These Variables can be Accessed from // Outside the Namespace Using the Scope Operator :: cout << first::val; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  22. 22. Use of Namespace #include <iostream> using namespace std; // Variable Created Inside Namespace namespace first { int val = 500; } // Global Variable int val = 100; int main() { // Local Variable int val = 200; // These Variables can be Accessed from // Outside the Namespace Using the Scope Operator :: cout << first::val; return 0; } Introduction to C++ Lecture Slides By Adil Aslam 500
  23. 23. Your Task.. #include <iostream> using namespace std; // Variable Created Inside Namespace namespace first { int val = 500; } int val = 100; // Global Variable int main() { int val = 200; // Local Variable cout << first::val<<endl; cout<<val<<endl; cout<<::val; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  24. 24. Your Task.. #include <iostream> using namespace std; // Variable Created Inside Namespace namespace first { int val = 500; } int val = 100; // Global Variable int main() { int val = 200; // Local Variable cout << first::val<<endl; cout<<val<<endl; cout<<::val; return 0; } Introduction to C++ Lecture Slides By Adil Aslam 500 200 100
  25. 25. Defining a Namespace • Namespace definition is similar to class definition, the only difference is it doesn't end with a semicolon like a class does. namespace keyword is used at the beginning like class is used while defining a class. • Syntax of Namespace Definition Introduction to C++ Lecture Slides By Adil Aslam namespace namespace_name { //body of namespace }
  26. 26. Defining a Namespace • The member can be accessed in the program as: • For Example: • Now, the variable a and function sum() can be accessed in the program as: Introduction to C++ Lecture Slides By Adil Aslam namespace_name::member_name; namespace sample { int a; int sum(int x, int y) { return (x+y); } } sample::a = 5; int x = sample::sum(5,9);
  27. 27. Rules to Create Namespace • The namespace definition must be done at global scope, or nested inside another namespace. • Namespace definition doesn't terminates with a semicolon like in class definition. • We cannot create instance of namespace. • Namespace declarations can be nested within another namespace. • Namespace declarations don’t have access specifiers. (Public or private) • No need to give semicolon after the closing brace of definition of namespace. • We can split the definition of namespace over several units. Introduction to C++ Lecture Slides By Adil Aslam
  28. 28. Example: Defining a Namespace-1/2 #include <iostream> using namespace std; // first namespace namespace first_space{ void func(){ cout << "Inside first_space" << endl; } } // second namespace namespace second_space{ void func(){ cout << "Inside second_space" << endl; } } Introduction to C++ Lecture Slides By Adil Aslam
  29. 29. Example: Defining a Namespace-2/2 int main () { // Calls function from first name space first_space::func(); // Calls function from second name space second_space::func(); return 0; } Introduction to C++ Lecture Slides By Adil Aslam Inside first_space Inside second_space
  30. 30. One More Example of Namespace-1/2 #include <iostream> using namespace std; namespace first { int i=20; float f=5.5; } namespace second { int i=20; float f=5.5; } Introduction to C++ Lecture Slides By Adil Aslam
  31. 31. One More Example of Namespace-2/2 int main() { int a; float b; a=first::i + second::i; cout<<"Addition of Two Numbers: "<<a; b=first::f * second::f; cout<<"n"; cout<<"Multiplication of Two Numbers: "<<b; } Introduction to C++ Lecture Slides By Adil Aslam
  32. 32. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  33. 33. Define a Function Outside the Namespace-1/2 #include <iostream> using namespace std; namespace math { int mul (int x, int y) { return x*y; } //Declaration of Function float add(float x, float y); } //Definition of the Function float math::add(float x, float y) { return x+y; } Introduction to C++ Lecture Slides By Adil Aslam
  34. 34. Define a Function Outside the Namespace-2/2 int main() { int a; float b; a=math::mul(5,6); cout<<"Multiplication of Two Numbers: "<<a; b=math::add(5.5,4.25); cout<<"n"; cout<<"Addition of Two Numbers: "<<b; } Introduction to C++ Lecture Slides By Adil Aslam
  35. 35. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  36. 36. You Know..?? #include <iostream> using namespace std; namespace math { int mul (int x, int y) { return x*y; } } int main() { int a; a=math::mul(5,6); cout<<"Multiplication of Two Numbers: "<<a; cout<<endl; } Introduction to C++ Lecture Slides By Adil Aslam If we don’t want to use this Namespace then what we can do ..??
  37. 37. Solution of Previous Situation #include <iostream> namespace math { int mul (int x, int y) { return x*y; } } int main() { int a; a=math::mul(5,6); std::cout<<"Multiplication of Two Numbers: "<<a; std::cout<<std::endl; } Introduction to C++ Lecture Slides By Adil Aslam Simply we Put std:: before Every std Class or Function
  38. 38. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  39. 39. Multiple Namespace Blocks with the Same Name Allowed • It’s legal to declare namespace blocks in multiple locations (either across multiple files, or multiple places within in the same file). All declarations within the namespace block are considered part of the namespace. • add.h: Introduction to C++ Lecture Slides By Adil Aslam namespace Math{ // function add() is part of namespace Math int add(int x, int y) { return x + y; } }
  40. 40. Multiple Namespace Blocks with the Same Name Allowed • subtract.h: • main.cpp: Introduction to C++ Lecture Slides By Adil Aslam namespace Math { int subtract(int x, int y) { return x - y; } } #include "add.h" // import Math::add() #include "subtract.h" // import Math::subtract() int main(void) { std::cout <<Math::add(4, 3) << 'n'; std::cout <<Math::subtract(4, 3) << 'n'; return 0; }
  41. 41. C++ Unnamed Namespace • A namespace without any name is called unnamed namespace. C++ allows programmer to create unnamed namespaces. After defining an unnamed namespace, its members can be accessed from any module of the program without using any qualification. They are usually created to shield global data. • Syntax of Unnamed Namespace Introduction to C++ Lecture Slides By Adil Aslam namespace { //body of unnamed namespace ... ... ... }
  42. 42. Unnamed Namespace • Unnamed namespaces are the replacement for the static declaration of variables. • They are directly usable in the same program and are used for declaring unique identifiers. • In unnamed namespaces, name of the namespace in not mentioned in the declaration of namespace. • The name of the namespace is uniquely generated by the compiler. • The unnamed namespaces you have created will only be accessible within the file you created it in. Introduction to C++ Lecture Slides By Adil Aslam
  43. 43. Example of Unnamed Namespace #include<iostream> using namespace std; namespace { void display(int x) { cout<<"You Enter = "<<x; } } int main() { int n; cout<<"Enter an Integer: "; cin>>n; display(n); return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  44. 44. Example of Unnamed Namespace #include<iostream> using namespace std; namespace { void display(int x) { cout<<"You Enter = "<<x; } } int main() { int n; cout<<"Enter an Integer: "; cin>>n; display(n); return 0; } Introduction to C++ Lecture Slides By Adil Aslam Unnamed Namespace Enter an Integer: 20 You Enter = 20
  45. 45. Using a Namespace • There are three ways to use a namespace in program, • 1: Scope Resolution • 2: The using directive • 3: The using declaration Introduction to C++ Lecture Slides By Adil Aslam
  46. 46. 1: With Scope Resolution • Any name (identifier) declared in a namespace can be explicitly specified using the namespace's name and the scope resolution :: operator with the identifier. Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace space{ void func(){ cout << “I am Function" ; } } int main () { //Accessing Member of Namespace Using :: space::func(); return 0; } I am Function
  47. 47. 2: The Using Directive • We can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace • using keyword allows you to import an entire namespace into your program with a global scope. • We can use ‘using’ directive to specify the namespace. Introduction to C++ Lecture Slides By Adil Aslam
  48. 48. 2: The Using Directive namespace first_space{ // First Namespace void func(){ cout << "First Namespace Function" << endl; } } namespace second_space{ // Second Namespace void func(){ cout << "Second Namespace Function" << endl; } } using namespace first_space; int main () { // This Calls Function From First Namespace func(); return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  49. 49. 2: The Using Directive namespace first_space{ // First Namespace void func(){ cout << "First Namespace Function" << endl; } } namespace second_space{ // Second Namespace void func(){ cout << "Second Namespace Function" << endl; } } using namespace first_space; int main () { // This Calls Function From First Namespace func(); return 0; } Introduction to C++ Lecture Slides By Adil Aslam Here We can Tell the Compiler to use “first_space” Namespace First Namespace Function
  50. 50. 3: The Using Declaration • The using directive can also be used to refer to a particular item within a namespace. • For example, if the only part of the std namespace that you intend to use is cout, you can refer to it as follows: • Subsequent code can refer to cout without prepending the namespace, but other items in the std namespace will still need to be explicit as follows: Introduction to C++ Lecture Slides By Adil Aslam using std::cout; #include <iostream> using std::cout; int main () { cout << "std::endl is used with std!" << std::endl; return 0; } std::endl is used with std!
  51. 51. The Using Declaration • Names introduced in a using directive obey normal scope rules. • The name is visible from the point of the using directive to the end of the scope in which the directive is found. • Entities with the same name defined in an outer scope are hidden. Introduction to C++ Lecture Slides By Adil Aslam
  52. 52. The Using Declaration #include<iostream> using namespace std; namespace first { int i; } namespace second { int i; } int main() { first::i=10; //scope resolution second::i=20; using first::i; //using declaration cout<<i; } Introduction to C++ Lecture Slides By Adil Aslam 10
  53. 53. The Using Declaration • NOTE : Name imported with using declaration can override the name imported with using directive Introduction to C++ Lecture Slides By Adil Aslam namespace X { void f() {} void g() {} } namespace Y { void f() {} void g() {} } int main() { using namespace X; // using directive using Y::f; // using declaration f(); // calls f() of Y namespace X::f(); // class f() of X namespace } In using Declaration, we Never Mention the Argument list of a Function while Importing it, Hence if a Namespace has Overloaded Function, it will Lead to Ambiguity.
  54. 54. Nested Namespaces in C++ • Defining a namespace inside another namespace is called nested namespace. • Syntax of Nested Namespace • The member declared inside nested namespace (namespace_name2) can be accessed as: Introduction to C++ Lecture Slides By Adil Aslam namespace namespace_name1 { //body of namespace_name1 ... ... ... namespace namespace_name2 { //body of namespace_name2 } ... ... ... } namespace_name1::namespace_name2::member_name;
  55. 55. Example of Nested Namespaces #include<iostream> using namespace std; namespace name1 { int a; float b; namespace name2 { float add(int x, float y) { return x+y; } } } int main() { cout<<"Enter a and b: "; cin>>name1::a>>name1::b; cout<<"Sum = "<<name1::name2::add(name1::a,name1::b)<<endl; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  56. 56. Example of Nested Namespaces #include<iostream> using namespace std; namespace name1 { int a; float b; namespace name2 { float add(int x, float y) { return x+y; } } } int main() { cout<<"Enter a and b: "; cin>>name1::a>>name1::b; cout<<"Sum = "<<name1::name2::add(name1::a,name1::b)<<endl; return 0; } Introduction to C++ Lecture Slides By Adil Aslam Outer Namespace Inner Namespace
  57. 57. Example of Nested Namespaces #include<iostream> using namespace std; namespace name1 { int a; float b; namespace name2 { float add(int x, float y) { return x+y; } } } int main() { cout<<"Enter a and b: "; cin>>name1::a>>name1::b; cout<<"Sum = "<<name1::name2::add(name1::a,name1::b)<<endl; return 0; } Introduction to C++ Lecture Slides By Adil Aslam Nested Namespace is used in this Program. A Namespace name2 is Defined inside Another Namespace name1. The Members of name1 i.e. a and b can be Accessed simply as name1::aand name1::b. But to Access the Members of name2 i.e. add(), we need to Specify both Namespaces name1 and name2. The add() Function can be Accessed as name1::name2::add() as shown in Program. Enter a and b: 13 27.892 Sum = 40.892
  58. 58. Nested Namespaces in C++ • We can also access members of nested namespace by using alternative way as follows: Introduction to C++ Lecture Slides By Adil Aslam namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } } // to access members of namespace_name2 using namespace namespace_name1::namespace_name2; // to access members of namespace:name1 using namespace namespace_name1;
  59. 59. Example of Nested Namespaces in C++ namespace first_space{ // First Namespace void func(){ cout << "Outer Namespace Function" << endl; } namespace second_space{ // Second Namespace void func(){ cout << "Inner Namespace Function" << endl; } } } using namespace first_space::second_space; int main () { // This calls Function From Second(Inner) Namespace. func(); return 0; } Introduction to C++ Lecture Slides By Adil Aslam Inner Namespace Function
  60. 60. Problem Statement • Write a program to illustrate nested namespaces. Also use the unnamed namespace and display the variable declare and initialized in it. Introduction to C++ Lecture Slides By Adil Aslam
  61. 61. Solution of the Previous Problem-1/2 #include <iostream> using namespace std; namespace Outer { int a=20; string s="Adil Aslam"; namespace Inner { int a=40; string s="Hina Adil"; } } namespace { //unnamed Namespace int x=60; char c='A'; } Introduction to C++ Lecture Slides By Adil Aslam
  62. 62. Solution of the Previous Problem-2/2 int main() { cout<<"Outer a= "<<Outer::a<<endl; cout<<"Outer s= "<<Outer::s<<endl; cout<<"Inner a= "<<Outer::Inner::a<<endl; cout<<"Inner s= "<<Outer::Inner::s<<endl; cout<<"Unnamed x= "<<x<<endl; cout<<"Unnamed c= "<<c<<endl; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  63. 63. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  64. 64. Namespace Alias • It is also possible to declare an alternate name for an existing namespace. • There is a possibility to use namespace alias. It can be useful when you have a very long namespace name or there are many nested namespaces. The use of alias has following syntax: • Example: Introduction to C++ Lecture Slides By Adil Aslam namespace newName = oldName or NestedNamespace. namespace MyNameIsAdilAslam{ void study(); class Learn { }; } // Adil is now Alias for MyNameIsAdilAslam namespace Adil = MyNameIsAdilAslam ;
  65. 65. Example of Namespace Alias #include <iostream> using namespace std; namespace ThisIsANameSpace{ void func(){ cout << "Hello Name Space" << endl; } } namespace Adil=ThisIsANameSpace; int main () { Adil::func(); return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  66. 66. Example of Namespace Alias #include <iostream> using namespace std; namespace ThisIsANameSpace{ void func(){ cout << "Hello Name Space" << endl; } } namespace Adil=ThisIsANameSpace; int main () { Adil::func(); return 0; } Introduction to C++ Lecture Slides By Adil Aslam Now New Name of “ThisIsANameSpace” Namespace is “Adil” Hello Name Space
  67. 67. Classes and Namespace • We Can Have Class Definition Inside Namespace Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace ns { // A Class in a namespace class Student { public: void display() { cout << "Class Member Function"; } }; } int main() { ns::Student obj; // Creating Object of Student Class obj.display(); return 0; } Class Member Function
  68. 68. Classes and Namespace • Class can also be declared inside namespace and defined outside namespace Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace ns{ class Student; // Only Declaring Class Here } class ns::Student // Defining Class Outside the Namespace { public: void display() { cout << "Class Member Function"; } }; int main() { ns::Student obj; //Creating Object of Student Class obj.display(); return 0; } Class Member Function
  69. 69. Classes and Namespace • We can define methods also outside the namespace Introduction to C++ Lecture Slides By Adil Aslam namespace ns { void display(); //Namespace Method Declaration class data { public: void display(); //Class Method Declaration }; } void ns::data::display() { // Defining Methods of Class cout << "ns::data::display()"; } void ns::display() { //Defining Method of Namespace cout << "ns::display()n"; } int main() { ns::data obj; ns::display(); obj.display(); return 0; } ns::data::display() ns::display()
  70. 70. Classes and Namespace-1/3 #include<iostream> #include<string.h> //require for gets function using namespace std; namespace sample { class person { private: char name[100]; char age[100]; char gender[10]; Introduction to C++ Lecture Slides By Adil Aslam
  71. 71. Classes and Namespace-2/3 public: void getdata() { cout<<"Name: "; gets(name); cout<<"Age: "; gets(age); cout<<"Gender: "; gets(gender); cout<<endl; } //end of getdata function void display() { cout<<"Name: "<<name<<endl; cout<<"Age: "<<age<<endl; cout<<"Gender : "<<gender<<endl<<endl; } //end of display function }; //end of person class } //end of sample namespace Introduction to C++ Lecture Slides By Adil Aslam
  72. 72. Classes and Namespace-3/3 int main() { sample::person p1; // Namespace Name is Required using namespace sample; person p2; // Namespace Name is not Required cout<<"Entering data for person p1"<<endl; p1.getdata(); cout<<"Entering data for person p2"<<endl; p2.getdata(); cout<<"Displaying data for person p1"<<endl; p1.display(); cout<<"Displaying data for person p2"<<endl; p2.display(); return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  73. 73. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  74. 74. Explanation of Previous Program • A class person is defined inside a namespace sample. • The attributes of person is name, age and gender and it has two functions, getdata() and display(). Inside main(), two objects of person are created. • Before using the ‘using namespace sample;‘ statement, we need to specify the name of namespace before class to create an object. But after using the using ‘namespace sample;’ statement, we no longer need to specify the name of namespace while using its members. Introduction to C++ Lecture Slides By Adil Aslam
  75. 75. Inline Namespaces • An inline namespace is a namespace that uses the optional keyword inline in its original-namespace- definition. Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace ns1 { inline namespace ns2 { int var = 10; } } int main() { cout << ns1::var; return 0; }
  76. 76. Inline Namespaces • An inline namespace is a namespace that uses the optional keyword inline in its original-namespace- definition. Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace ns1 { inline namespace ns2 { int var = 10; } } int main() { cout << ns1::var; return 0; } We can see in this Example that Members of an inline Namespace are Treated as they are Members of the Enclosing Namespace in many Situations (listed in next slide). This Property is Transitive: if a Namespace N contains an inline namespace M, which in turn Contains an inline namespace O, then the Members of O can be used as though they were Members of M or N. 10
  77. 77. Inline Namespaces #include <iostream> using namespace std; namespace ns1 { inline namespace ns2 { inline namespace ns3 { int var = 10; } } } int main() { cout << ns1::var; return 0; } Introduction to C++ Lecture Slides By Adil Aslam The inline Specifier makes the Declarations from the Nested Namespace Appear Exactly as if they had been Declared in the Enclosing Namespace. This means it Drags out the Declaration (“var” in the this example) from a Nested Namespace to the Containing Namespace. 10
  78. 78. Inline Namespaces and Usage of the “Using” Directive Inside Namespaces Advantages of using inline namespaces: • Avoid verbose :Consider the above code, if you want to print “var”, you write: • This looks good only if namespace's names are short as in the above example. But by using inline with namespaces there is no need to type the entire namespace as given above or use the "using" directive. • Support of Library :The inline namespace mechanism is intended to support library evolution by providing a mechanism that supports a form of versioning. Refer this for details. Introduction to C++ Lecture Slides By Adil Aslam cout << ns1::ns2::ns3::var; "Using" directive
  79. 79. Inline Namespaces and Using Directive • This same behavior we already discuss (same as inline namespaces) can be achieved by using the "using" declarative inside namespaces. A using-directive that names the inline namespace is implicitly inserted in the enclosing namespace (similar to the implicit using-directive for the unnamed namespace). Consider the following C++ code: Introduction to C++ Lecture Slides By Adil Aslam namespace ns1 { namespace ns2 { namespace ns3 { int var = 10; } using namespace ns3; } using namespace ns2; } int main() { cout << ns1::var; return 0; } 10
  80. 80. Introduction to C++ Lecture Slides By Adil Aslam Preprocessors in C++
  81. 81. Preprocessors in C++ • The preprocessor include the instructions for the compiler, these instructions are executed before the source code is compiled. • Preprocessor directives begin with hash sign(#). • No semicolon (;) is expected at the end of a preprocessor directive. • We already have seen a #include directive in all the examples. This macro is used to include a header file into the source file. • There are number of preprocessor directives supported by C++ like #include, #define, #if, #else, #line, etc. Introduction to C++ Lecture Slides By Adil Aslam
  82. 82. Preprocessors in C++ Introduction to C++ Lecture Slides By Adil Aslam Directive Description #define Substitutes a preprocessor macro #include Inserts a particular header from another file #undef Undefines a preprocessor macro #ifdef Returns true if this macro is defined #ifndef Returns true if this macro is not defined #if Tests if a compile time condition is true #else The alternative for #if #elif #else an #if in one statement #endif Ends preprocessor conditional #error Prints error message on stderr #pragma Issues special commands to the compiler, using a standardized method
  83. 83. The #define Preprocessor Directive as Constant • The #define preprocessor directive is used to define constant values in the program. • The symbolic constant is called a macro. • Syntax of #define Preprocessor Directive • The #define preprocessor directive define the macro-name and value, this value is replaced with each time the macro-name appears in the program Introduction to C++ Lecture Slides By Adil Aslam #define macro_name value
  84. 84. Example of #define Preprocessor Directive 1 #include <iostream> #define PI 3.14 using namespace std; int main() { int r; float area; cout << "Enter Radius : "; cin >> r; area = PI * r * r; cout << "nArea of Circle is : " << area; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  85. 85. Example of #define Preprocessor Directive 1 #include <iostream> #define PI 3.14 using namespace std; int main() { int r; float area; cout << "Enter Radius : "; cin >> r; area = PI * r * r; cout << "nArea of Circle is : " << area; return 0; } Introduction to C++ Lecture Slides By Adil Aslam Here #define use for Constant Value and PI is macro-name Enter Radius: 2 Area of Circle is : 12.56
  86. 86. Example of #define Preprocessor Directive 2 #include <iostream> #define SIZE 5 using namespace std; int main() { int arr[SIZE]; for(int i=0;i<SIZE;i++) { cout << "Enter any Number : "; cin >> arr[i]; } for(int i=0;i<SIZE;i++) cout << arr[i] << ", "; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  87. 87. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  88. 88. The #define Preprocessor Directive as Function • The #define preprocessor directive can be used as inline function which is replaced at compile time. • The definition of macro accepts an argument and that argument is replaced by the actual argument found in program. • Example • Here r is used as a argument of this Macros Introduction to C++ Lecture Slides By Adil Aslam #define area(r) (3.14*r*r)
  89. 89. Example of #define Preprocessor as Function 1 #include <iostream> #define area(r) (3.14*r*r) using namespace std; int main() { int radius; float area; cout << "Enter Radius of Circle : "; cin >> radius; area = area(radius); cout << "Area of circle is : " << area << endl; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  90. 90. Example of #define Preprocessor as Function 1 #include <iostream> #define area(r) (3.14*r*r) using namespace std; int main() { int radius; float area; cout << "Enter Radius of Circle : "; cin >> radius; area = area(radius); cout << "Area of circle is : " << area << endl; return 0; } Introduction to C++ Lecture Slides By Adil Aslam Function-Like Macros
  91. 91. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  92. 92. Explanation of the Previous Program • We have defined a macro 'area' which takes one argument that is 'r'. So when we called area(radius), it got replaced by 3.14*radius*radius. • It is like we have defined something and whenever it will appear in our program, will be replaced by that definition. Introduction to C++ Lecture Slides By Adil Aslam
  93. 93. Example of #define Preprocessor as Function 2 #include <iostream> #define MIN(a,b) (((a)<(b)) ? a : b) using namespace std; int main () { int i, j; i = 100; j = 30; cout<<"The Minimum is : "<<MIN(i, j)<<endl; return 0; } Introduction to C++ Lecture Slides By Adil Aslam The Minimum is : 30
  94. 94. Example of #define Preprocessor as Function 3 #include <iostream> #define SQUARE(x) ((x) *(x)) using namespace std; int main() { int num; cout << "Enter any Number : "; cin >> num; cout << "The Square is : " << SQUARE(num); return 0; } Introduction to C++ Lecture Slides By Adil Aslam Enter any Number : 5 The Square is : 25
  95. 95. Difference Between Macros and Functions Introduction to C++ Lecture Slides By Adil Aslam Macros Functions Macro calls are replaced with macro expression. In function call, the control is passed to a function definition along with arguments, and definition is processed and value may be returned to call. Macros run programs faster but increase the program size. Functions run programs slower but decrease the program size and compact. It is better to use Macros, when definition is very small in size. It is better to use Functions, when definition is bigger in size.
  96. 96. Mix Example of #define #include <iostream> #define PI 3.14 #define SQR(X) ((X)* (X)) #define CUBE(y) SQR(y)* (y) using namespace std; int main() { int radius; cout << endl << "Enter Radius of the Circle: "; cin >> radius; cout << endl << "Area of the Circle is: " << PI * SQR(radius); cout << endl << "Perimeter of Circle is : " << 2 * PI * radius; cout << endl << "Area of the Sphere of Radius " << radius << " is: " << 4/3.0 * PI * CUBE(radius); } Introduction to C++ Lecture Slides By Adil Aslam
  97. 97. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  98. 98. Some Important Points • We do not terminate the macros using (;) • Macros can be parameterized • Macros can be dependent on other macros (must be defined before they are used.) • Macros are customarily written in capital letters to avoid confusion between macro and functions. (this is not a rule) • Parameters are enclosed in () to avoid the ambiguity arising due to operator precedence. For example if we call SQR(4+3). This statement will be expanded as (4+3) * (4+3). Introduction to C++ Lecture Slides By Adil Aslam
  99. 99. Some Important Points • Directives are helpful whenever things are prone to changes and are used at many places in one program. Changing the directive value at the top will cause the corresponding changes at all the places in the program. • If the directive requires use of more than one line then it can be continued in the next line after placing ‘’ at the end of the first line. Introduction to C++ Lecture Slides By Adil Aslam
  100. 100. Directive in Two Lines #include <iostream> #define greater(m,n)(m>n) ? (m) : (n); using namespace std; int main() { int number1, number2; cout << endl << "Enter two Numbers:n"; cin >> number1 >> number2; int g = greater(number1, number2); cout << endl << "Greater Numbers is: " << g; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  101. 101. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  102. 102. Please Note • A macro with arguments has its arguments substituted for replacement text, when the macro is expanded. • A macro substitutes text only; it does not check for data types. • While defining macros, make sure to use parenthesis, as it ensures the correct result. For instance, if you have defined a macro like : Introduction to C++ Lecture Slides By Adil Aslam #define CIRCLE_AREA(x) PI∗x∗x and you are using it as : area = CIRCLE_AREA(c + 2) ; then it would be expanded as : area = 3.14159 ∗ c + 2 ∗ c + 2 ;
  103. 103. Please Note • See, would it yield the correct result? To obtain the correct result, you should define this macro as : • Remember: • definition of symbolic constants and macros through # define directive. Introduction to C++ Lecture Slides By Adil Aslam #define CIRCLE_AREA((x)) PI∗(x)∗(x)
  104. 104. The #include Preprocessor Directive • The #include preprocessor directive is used to include another source file in our source code. This is commonly used to include header files in our program. • Syntax of #include Preprocessor Directive • The source file to be added must be enclosed within double quotes (" ") or angle brackets (< >) . If the file is enclosed in double quotes the search will be done in current directory. If the file is enclosed in angle brackets the search will be done in standard directories (include directory) where the libraries are stored. Introduction to C++ Lecture Slides By Adil Aslam #include "filename.h" or #include <filename.h>
  105. 105. Conditional Compilation Directives • Conditional Compilation Directives these are used to execute statements conditionally for: • Executing the code on different machine architectures. • Debugging purposes. • Macros can be dependent on other macros (must be defined before they are used.) • Evaluating codes, which are to be executed depending upon the requirements of the programmer. • The following directives are included in this category: • #if , #elif , #endif , #ifdef , #ifndef Introduction to C++ Lecture Slides By Adil Aslam
  106. 106. The #if-#else-#endif Preprocessor Directive • The #if preprocessor directive takes condition in parenthesis, if condition is true, then the statements given between #if and #else will get execute. If condition is false, then statements given between #else and #endif will get execute. • Syntax of #if-#else-#endif Preprocessor Directive Introduction to C++ Lecture Slides By Adil Aslam #if(condition) - - - - - - - - - - - - - - - - - - - - - - #else - - - - - - - - #endif
  107. 107. Example of #if-#else-#endif Preprocessor Directive #include <iostream> #define MAX 100 using namespace std; int main() { #if MAX > 50 cout << "Yes, MAX is Greater then 100."; #else cout << "No, MAX is not Greater then 100."; #endif return 0; } Introduction to C++ Lecture Slides By Adil Aslam Yes, MAX is Greater then 100.
  108. 108. The #elif Preprocessor Directive • The #elif preprocessor directive is similar to if-else ladder statement. It is used for checking multiple conditions, if the first condition will not satisfy, compiler will jump to #else block and check other condition is true or not and so on. • Syntax of #if-#elif-#else-#endif Preprocessor Directive Introduction to C++ Lecture Slides By Adil Aslam #if(condition) - - - - - - - - - - #elif(condition) - - - - - - - - - - #elif(condition) - - - - - - - - - - #else - - - - - - - - - - #endif
  109. 109. Example of #if-#elif-#else-#endif Preprocessor Directive #include <iostream> #define MKS 65 using namespace std; int main() { #if MKS>=80 cout << "nGrade A"; #elif MKS>=70 cout << "nGrade B"; #elif MKS>=60 cout << "nGrade C"; #else cout << "nGrade D"; #endif } Introduction to C++ Lecture Slides By Adil Aslam Grade C
  110. 110. The #ifdef Preprocessor Directive • The #ifdef preprocessor directive is used to check whether the macro-name is previously defined or not, if defined then the statements given between #ifdef and #else will get execute. • Syntax of #ifdef Preprocessor Directive Introduction to C++ Lecture Slides By Adil Aslam #ifdef macro-name - - - - - - - - - - - - - - - - - - - - #else - - - - - - - - - - - - - - - - - - - - #endif
  111. 111. Example of #ifdef Preprocessor Directive #include <iostream> #define HELLO 65 using namespace std; int main() { #ifdef HELLO cout << "nHELLO is defined."; #else cout << "nHELLO is not defined."; #endif return 0; } Introduction to C++ Lecture Slides By Adil Aslam HELLO is defined.
  112. 112. #include <iostream> #define DEBUG #define MIN(num1, num2) (((num1)<(num2))?num1:num2) using namespace std; int main() { int a, b; cout<<"Enter two Number: "; cin>>a>>b; #ifdef DEBUG cerr<<"Trace: inside the main() Functionn"; #endif #if 0 cout<<MKSTR(HELLO C++)<<endl; //This is commented part #endif cout<<"The Minimum = "<<MIN(a, b)<<"n"; #ifdef DEBUG cerr<<"Trace: Coming out of the main() Functionn"; #endif return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  113. 113. #include <iostream> #define DEBUG #define MIN(num1, num2) (((num1)<(num2))?num1:num2) using namespace std; int main() { int a, b; cout<<"Enter two Number: "; cin>>a>>b; #ifdef DEBUG cerr<<"Trace: inside the main() Functionn"; #endif #if 0 cout<<MKSTR(HELLO C++)<<endl; //This is commented part #endif cout<<"The Minimum = "<<MIN(a, b)<<"n"; #ifdef DEBUG cerr<<"Trace: Coming out of the main() Functionn"; #endif return 0; } Introduction to C++ Lecture Slides By Adil Aslam The cerr Statement to be Compiled in the Program if the Symbolic Constant DEBUG has been Defined before Directive #ifdef DEBUG.
  114. 114. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  115. 115. The #undef Preprocessor Directive #include <iostream> #define PI 3.14 #define SQR(X) ((X)* (X)) #define CUBE(y) SQR(y)* (y) using namespace std; int main() { int radius; cout << endl << "Enter Radius of the Circle: "; cin >> radius; cout << endl << "Area of the Circle is: " << PI * SQR(radius); #undef PI //It is now invalid to use PI, thus we cannot use the next line. //cout << endl << "Perimeter of Circle is : " << 2 * PI * radius; cout << endl << "Area of the Sphere of Radius " << radius << " is: " << 4/3.0 * 3.14 * CUBE(radius); } Introduction to C++ Lecture Slides By Adil Aslam
  116. 116. The #undef Preprocessor Directive #include <iostream> #define PI 3.14 #define SQR(X) ((X)* (X)) #define CUBE(y) SQR(y)* (y) using namespace std; int main() { int radius; cout << endl << "Enter Radius of the Circle: "; cin >> radius; cout << endl << "Area of the Circle is: " << PI * SQR(radius); #undef PI //It is now invalid to use PI, thus we cannot use the next line. //cout << endl << "Perimeter of Circle is : " << 2 * PI * radius; cout << endl << "Area of the Sphere of Radius " << radius << " is: " << 4/3.0 * 3.14 * CUBE(radius); } Introduction to C++ Lecture Slides By Adil Aslam #undef – Used to undefine a macro (The macro cannot be used after it is undefined.)
  117. 117. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  118. 118. The #ifndef Preprocessor Directive • The #ifndef preprocessor directive is used to check whether the macro-name is previously defined or not, if not defined then the statements given between #ifndef and #else will get execute. • Syntax of #ifndef Preprocessor Directive Introduction to C++ Lecture Slides By Adil Aslam #ifndef macro-name - - - - - - - - - - - - - - - - - - - - #else - - - - - - - - - - - - - - - - - - - - #endif
  119. 119. Example of #ifndef Preprocessor Directive #include <iostream> #define MKS 65 int main() { #ifndef MAX cout << "nMAX is not defined."; #else cout << "nMAX is defined."; #endif return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  120. 120. Example of #ifndef Preprocessor Directive #include <iostream> #define MKS 65 int main() { #ifndef MAX cout << "nMAX is not defined."; #else cout << "nMAX is defined."; #endif return 0; } Introduction to C++ Lecture Slides By Adil Aslam Note: The #ifdef and #ifndef can not use the #elif Preprocessor Directive. MAX is not defined.
  121. 121. The #error Preprocessor Directive • The #error preprocessor directive is used to force the compile to stop compiling the source code. In other words we can manually force the compiler to stop compiling and give fatal error. • When the #error directive is found the program will be terminated and the message (that is given after the #error statement) will be used in the compilation error that is given by the compiler. Introduction to C++ Lecture Slides By Adil Aslam
  122. 122. Example of #error Preprocessor Directive 1 #include <iostream> using namespace std; int main() { #ifndef MAX //Statement 1 cout << "nMAX is not Defined."; #else cout << "nMAX is Defined."; #endif cout << "nEnd of Program."; return 0; } Introduction to C++ Lecture Slides By Adil Aslam Max is not Define. End of Program.
  123. 123. Example of #error Preprocessor Directive 2 #include <iostream> using namespace std; int main() { #ifndef MAX //Statement 1 #error MAX is not defined. #else cout << "nMAX is Defined."; #endif cout << "nEnd of Program."; return 0; } Introduction to C++ Lecture Slides By Adil Aslam The only Difference Between Example 1 and 2 is the Statement 1. In Example 1, Statement 1 will Display Message and Continue to Compile the rest of the code. In Example 2, Statement 1 will force the Compiler to give Fatal error and stop Compiling the rest of the Code.
  124. 124. The #pragma Preprocessor Directive • The #pragma directive is used to allow suppression of specific error messages, manage heap and stack debugging, etc. These are compiler specific directives. (Check your compiler documentation for the #pragma lines you can use.) • For Example “#pragma inline” statement indicates the assembly code is available in the code. • If the directive is unavailable in an implementation the statement is ignored. Introduction to C++ Lecture Slides By Adil Aslam
  125. 125. Predefined Macros-1/2 • ANSI C defines a number of macros. Although each one is available for your use in programming, the predefined macros should not be directly modified. Introduction to C++ Lecture Slides By Adil Aslam Macro Description __DATE__ The current date as a character literal in "MMM DD YYYY" format __TIME__ The current time as a character literal in "HH:MM:SS" format _FILE__ This contains the current filename as a string literal. __LINE__ This contains the current line number as a decimal constant. __STDC__ Defined as 1 when the compiler complies with the ANSI standard.
  126. 126. Predefined Macros-2/2 • ANSI C defines a number of macros. Although each one is available for your use in programming, the predefined macros should not be directly modified. Introduction to C++ Lecture Slides By Adil Aslam Macro Description __OBJC__ If this macro is defined with a value of 1 then the Objective-C compiler is in use. This macro can be used to test whether a header file is compiled by a C compiler or an Objective-C compiler. __ASSEMBLER__ This macro is defined with a value of 1 when preprocessing assembly language. __cplusplus__ Contains a 6 digit number to indicate that the compiler is based on the standards, otherwise it contains 5 or fewer digits.
  127. 127. Example of Predefined Macros #include <iostream> using namespace std; int main () { cout << "Value of __LINE__ : " << __LINE__ << endl; cout << "Value of __FILE__ : " << __FILE__ << endl; cout << "Value of __DATE__ : " << __DATE__ << endl; cout << "Value of __TIME__ : " << __TIME__ << endl; cout << "Value of __STDC__ : " << __STDC__ << endl; return 0; } Introduction to C++ Lecture Slides By Adil Aslam
  128. 128. Output of the Previous Program Introduction to C++ Lecture Slides By Adil Aslam
  129. 129. The # and ## Operators • The # operator causes a replacement-text token to be converted to a string surrounded by the quotes. Here is an example demonstrating this. Introduction to C++ Lecture Slides By Adil Aslam #include<iostream> #define MKSTR(z) #z using namespace std; int main() { cout<<MKSTR(Adil Aslam)<<endl; return 0; } Adil Aslam
  130. 130. The # and ## Operators • Here the line: • Becomes • The ## operator is used to concatenate two tokens. Next is an example program demonstrating this concept: Introduction to C++ Lecture Slides By Adil Aslam cout<<MKSTR(Adil Aslam)<<endl; cout<<"Adil Aslam"<<endl;
  131. 131. The # and ## Operators #include<iostream> #define concat(i, j) i##j using namespace std; int main() { int ab=100; cout<<concat(a, b); return 0; } Introduction to C++ Lecture Slides By Adil Aslam 100 Here the line cout<<concat(a, b); becomes cout<<ab;
  132. 132. Quiz Time .. • Which operator is used to signify the namespace? • a) conditional operator • b) ternary operator • c) scope operator • d) none of the mentioned Introduction to C++ Lecture Slides By Adil Aslam
  133. 133. Quiz Time .. • Which operator is used to signify the namespace? • a) conditional operator • b) ternary operator • c) scope operator • d) none of the mentioned Introduction to C++ Lecture Slides By Adil Aslam Answer : c
  134. 134. Quiz Time .. • Identify the correct statement. • a) Namespace is used to group class, objects and functions. • b) Namespace is used to mark the beginning of the program. • c) Namespace is used to separate the class, objects. • d) None of the above Introduction to C++ Lecture Slides By Adil Aslam
  135. 135. Quiz Time .. • Identify the correct statement. • a) Namespace is used to group class, objects and functions. • b) Namespace is used to mark the beginning of the program. • c) Namespace is used to separate the class, objects. • d) None of the above • Explanation: Namespace allow you to group class, objects and functions. It is used to divide the global scope into the sub-scopes. Introduction to C++ Lecture Slides By Adil Aslam Answer : a
  136. 136. Quiz Time .. • What is the use of Namespace? • a) To encapsulate the data • b) To structure a program into logical units. • c) Both a and b • d) none of the mentioned Introduction to C++ Lecture Slides By Adil Aslam
  137. 137. Quiz Time .. • What is the use of Namespace? • a) To encapsulate the data • b) To structure a program into logical units. • c) Both a and b • d) none of the mentioned • Explanation: The main aim of the namespace is to understand the logical units of the program and to make the program so robust. Introduction to C++ Lecture Slides By Adil Aslam Answer : b
  138. 138. Quiz Time .. • What is the general syntax for accessing the namespace variable? • a) namespaceid::operator • b) namespace,operator • c) namespace#operator • d) none of the mentioned Introduction to C++ Lecture Slides By Adil Aslam
  139. 139. Quiz Time .. • What is the general syntax for accessing the namespace variable? • a) namespaceid::operator • b) namespace,operator • c) namespace#operator • d) none of the mentioned Introduction to C++ Lecture Slides By Adil Aslam Answer : a
  140. 140. Quiz Time .. • What is the output of this program? Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace first { int var = 5; } namespace second { double var = 3.1416; } int main () { int a; a = first::var + second::var; cout << a; return 0; } a) 8.31416 b) 8 c) 9 d) compile time error
  141. 141. Quiz Time .. • What is the output of this program? Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace first { int var = 5; } namespace second { double var = 3.1416; } int main () { int a; a = first::var + second::var; cout << a; return 0; } a) 8.31416 b) 8 c) 9 d) compile time error Answer : b Note: We Store Result in Integer Variable
  142. 142. Quiz Time .. • What is the output of this program? Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace Box1 { int a = 4; } namespace Box2 { int a = 13; } int main () { int a = 16; Box1::a; Box2::a; cout << a; return 0; } a) 4 b) 13 c) 16 d) compile time error
  143. 143. Quiz Time .. • What is the output of this program? Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace Box1 { int a = 4; } namespace Box2 { int a = 13; } int main () { int a = 16; Box1::a; Box2::a; cout << a; return 0; } a) 4 b) 13 c) 16 d) compile time error Answer : c Explanation: In this Program, as there is lot of Variable a and it is Printing the value inside the main block because it got the Highest Priority.
  144. 144. Quiz Time .. • What is the output of this program? Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace space { int x = 10; } namespace space { int y = 15; } int main() { space::x = space::y =5; cout << space::x << space::y; return 0; } a) 1015 b) 1510 c) 55 d) compile time error
  145. 145. Quiz Time .. • What is the output of this program? Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace space { int x = 10; } namespace space { int y = 15; } int main() { space::x = space::y =5; cout << space::x << space::y; return 0; } a) 1015 b) 1510 c) 55 d) compile time error Answer : c Explanation: We are Overriding the value at the main Function and so we are Getting the output as 55.
  146. 146. Quiz Time .. • What is the output of this program? Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace extra { int i; } void i() { using namespace extra; int i; i = 9; cout << i; } int main() { enum letter { i, j}; class i { letter j; }; ::i(); } 9 b) 10 c) compile time error d) none of the mentioned
  147. 147. Quiz Time .. • What is the output of this program? Introduction to C++ Lecture Slides By Adil Aslam #include <iostream> using namespace std; namespace extra { int i; } void i() { using namespace extra; int i; i = 9; cout << i; } int main() { enum letter { i, j}; class i { letter j; }; ::i(); } 9 b) 10 c) compile time error d) none of the mentioned Answer : a Explanation: A scope resolution operator without a scope qualifier refers to the global namespace.
  148. 148. Quiz Time .. • Which keyword is used to access the variable in namespace? • a) using • b) dynamic • c) const • d) static Introduction to C++ Lecture Slides By Adil Aslam
  149. 149. Quiz Time .. • Which keyword is used to access the variable in namespace? • a) using • b) dynamic • c) const • d) static Introduction to C++ Lecture Slides By Adil Aslam Answer : a
  150. 150. In Upcoming Lecture We Learn • Exception Handling in C++ • Why Exception Handling? • Examples of Exception Handling in C++ • Rethrowing Exceptions in C++ • C++ Standard Exceptions • Define New Exceptions in C++ • Exception Handling and Inheritance in C++ • Exceptions in Constructors and Destructors in C++ • Class Templates With Exception Handling in C++ • Advantages of Exception Handling Introduction to C++ Lecture Slides By Adil Aslam
  151. 151. Thank You  Introduction to C++ Lecture Slides By Adil Aslam

×