SlideShare a Scribd company logo
1 of 17
Inner And Nested Classes
Bilal H. Rasheed MSc.(C.S)
Nov. 2019
Nested classes
• Nested class is a class defined inside a class,
that can be used within the scope of the class
in which it is defined. In C++ nested classes are
not given importance because of the strong
and flexible usage of inheritance. Its objects
are accessed using "Nest::Display".
Example:
• #include <iostream.h>
using name space std();
class Nest
{
public:
class Display
{
private:
int s;
public:
void sum( int a, int b)
{ s =a+b; }
void show( )
{ cout << "nSum of a and b is:: " << s;}
};
};
void main()
{
Nest::Display x;
x.sum(12, 10);
x.show();
}
Result:
• Sum of a and b is::22
In the above example, the nested class
"Display" is given as "public" member of the
class "Nest".
Local classes in C++
• Local class is a class defined inside a function.
Following are some of the rules for using these
classes.
• Global variables declared above the function can be
used with the scope operator "::".
• Static variables declared inside the function can also
be used.
• Automatic local variables cannot be used.
• It cannot have static data member objects.
• Member functions must be defined inside the local
classes.
• Enclosing functions cannot access the private
member objects of a local class.
• A class declared inside a function becomes local to that function and is called Local
Class in C++. For example, in the following program, Test is a local class in fun().
#include<iostream>
• using namespace std;
•
• void fun()
• {
• class Test // local to fun
• {
• /* members of Test class */
• };
• }
•
• int main()
• {
• return 0;
• }
•
Following are some interesting facts about local classes.
1) A local class type name can only be used in the enclosing function. For example, in the
following program, declarations of t and tp are valid in fun(), but invalid in main().
• #include<iostream>
• using namespace std;
•
• void fun()
• {
• // Local class
• class Test
• {
• /* ... */
• };
•
• Test t; // Fine
• Test *tp; // Fine
• }
•
• int main()
• {
• Test t; // Error
• Test *tp; // Error
• return 0;
• }
2) All the methods of Local classes must be defined inside the class only. For example, program 1
works fine and program 2 fails in compilation.• // PROGRAM 1
• #include<iostream>
• using namespace std;
•
• void fun()
• {
• class Test // local to fun
• {
• public:
•
• // Fine as the method is defined inside the local class
• void method() {
• cout << "Local Class method() called";
• }
• };
•
• Test t;
• t.method();
• }
•
• int main()
• {
• fun();
• return 0;
• }
• Output:
• Local Class method() called
• // PROGRAM 2
• #include<iostream>
• using namespace std;
•
• void fun()
• {
• class Test // local to fun
• {
• public:
• void method();
• };
•
• // Error as the method is defined outside the local class
• void Test::method()
• {
• cout << "Local Class method()";
• }
• }
•
• int main()
• {
• return 0;
• }
• Output:
• Compiler Error: In function 'void fun()': error: a function-definition is not allowed here before '{' token
3) A Local class cannot contain static data members. It may contain static functions
though. For example, program 1 fails in compilation, but program 2 works fine.
• // PROGRAM 1
• #include<iostream>
• using namespace std;
•
• void fun()
• {
• class Test // local to fun
• {
• static int i;
• };
• }
•
• int main()
• {
• return 0;
• }
• Compiler Error: In function 'void fun()': error: local class 'class fun()::Test' shall
not have static data member 'int fun()::Test :: program will given …
• // PROGRAM 2
• #include<iostream>
• using namespace std;
•
• void fun()
• {
• class Test // local to fun
• {
• public:
• static void method()
• {
• cout << "Local Class method() called";
• }
• };
•
• Test::method();
• }
•
• int main()
• {
• fun();
• return 0;
• }
• Output:
• Local Class method() called
4) Member methods of local class can only access static and enum variables of the enclosing
function. Non-static variables of the enclosing function are not accessible inside local classes. For
example, the program 1 compiles and runs fine. But, program 2 fails in compilation.
• // PROGRAM 1
• #include<iostream>
• using namespace std;
•
• void fun()
• {
• static int x;
• enum {i = 1, j = 2};
•
• // Local class
• class Test
• {
• public:
• void method() {
• cout << "x = " << x << endl; // fine as x is static
• cout << "i = " << i << endl; // fine as i is enum
• }
• };
•
• Test t;
• t.method();
• }
•
• int main()
• {
• fun();
• return 0;
• }
• Output:
• x = 0 i = 1
• // PROGRAM 2
• #include<iostream>
• using namespace std;
•
• void fun()
• {
• int x;
•
• // Local class
• class Test
• {
• public:
• void method() {
• cout << "x = " << x << endl;
• }
• };
•
• Test t;
• t.method();
• }
•
• int main()
• {
• fun();
• return 0;
• }
• Output:
• In member function 'void fun()::Test::method()': error: use of 'auto' variable from containing function
5) Local classes can access global types, variables and functions. Also, local classes can access
other local classes of same function.. For example, following program works fine.
• #include<iostream>
• using namespace std;
•
• int x;
•
• void fun()
• {
• // First Local class
• class Test1 {
• public:
• Test1() { cout << "Test1::Test1()" << endl; }
• };
•
• // Second Local class
• class Test2
• {
• // Fine: A local class can use other local classes of same function
• Test1 t1;
• public:
• void method() {
• // Fine: Local class member methods can access global variables.
• cout << "x = " << x << endl;
• }
• };
•
• Test2 t;
• t.method();
• }
• int main()
• {
• fun();
• return 0;
• }
• Output:
• Test1::Test1() x = 0
Example:
• #include <iostream.h>
using name space std();
int y;
void g();
int main()
{
g();
return 0;
}
void g()
{
class local
{
public:
void put( int n) {::y=n;}
int get() {return ::y;}
}ab;
ab.put(9.5);
cout << "The value assigned to y is::"<< ab.get();
};
Result:
• The value assigned to y is::9
In the above example, the local class "local" uses
the variable "y" which is declared globally. Inside
the function it is used using the "::" operator. The
object "ab" is used to set, get the assigned values
in the local class.

More Related Content

What's hot

20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism Intro C# Book
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIAjit Nayak
 
Functional programming in kotlin with Arrow [Sunnytech 2018]
Functional programming in kotlin with Arrow [Sunnytech 2018]Functional programming in kotlin with Arrow [Sunnytech 2018]
Functional programming in kotlin with Arrow [Sunnytech 2018]Emmanuel Nhan
 
C# / Java Language Comparison
C# / Java Language ComparisonC# / Java Language Comparison
C# / Java Language ComparisonRobert Bachmann
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories Intro C# Book
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console ProgramHock Leng PUAH
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)jeffz
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classesIntro C# Book
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm ComplexityIntro C# Book
 

What's hot (19)

20.5 Java polymorphism
20.5 Java polymorphism 20.5 Java polymorphism
20.5 Java polymorphism
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Functional programming in kotlin with Arrow [Sunnytech 2018]
Functional programming in kotlin with Arrow [Sunnytech 2018]Functional programming in kotlin with Arrow [Sunnytech 2018]
Functional programming in kotlin with Arrow [Sunnytech 2018]
 
C# / Java Language Comparison
C# / Java Language ComparisonC# / Java Language Comparison
C# / Java Language Comparison
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 
class and objects
class and objectsclass and objects
class and objects
 
Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)Why Java Sucks and C# Rocks (Final)
Why Java Sucks and C# Rocks (Final)
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
 

Similar to Topic 3

class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxAshrithaRokkam
 
introduction to c #
introduction to c #introduction to c #
introduction to c #Sireesh K
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxurvashipundir04
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptxKrutikaWankhade1
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructorsanitashinde33
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxNidhi Mehra
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptxAtharvPotdar2
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxrayanbabur
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slidesluqman bawany
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introductionSireesh K
 
Object oriented programming (first)
Object oriented programming (first)Object oriented programming (first)
Object oriented programming (first)Hvatrex Hamam
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxurvashipundir04
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.Tarunsingh198
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&deleteShehzad Rizwan
 

Similar to Topic 3 (20)

Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
class and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptxclass and object C++ language chapter 2.pptx
class and object C++ language chapter 2.pptx
 
introduction to c #
introduction to c #introduction to c #
introduction to c #
 
UNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptxUNIT3 on object oriented programming.pptx
UNIT3 on object oriented programming.pptx
 
Constructor
ConstructorConstructor
Constructor
 
C++.pptx
C++.pptxC++.pptx
C++.pptx
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
sSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptxsSCOPE RESOLUTION OPERATOR.pptx
sSCOPE RESOLUTION OPERATOR.pptx
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
 
Lecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptxLecture-11 Friend Functions and inline functions.pptx
Lecture-11 Friend Functions and inline functions.pptx
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introduction
 
Object oriented programming (first)
Object oriented programming (first)Object oriented programming (first)
Object oriented programming (first)
 
Inheritance
InheritanceInheritance
Inheritance
 
concepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptxconcepts of object and classes in OOPS.pptx
concepts of object and classes in OOPS.pptx
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Java For Automation
Java   For AutomationJava   For Automation
Java For Automation
 
C#2
C#2C#2
C#2
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
 

Recently uploaded

Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfSumit Kumar yadav
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Patrick Diehl
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisDiwakar Mishra
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksSérgio Sacani
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡anilsa9823
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptMAESTRELLAMesa2
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...Sérgio Sacani
 
Types of different blotting techniques.pptx
Types of different blotting techniques.pptxTypes of different blotting techniques.pptx
Types of different blotting techniques.pptxkhadijarafiq2012
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxAArockiyaNisha
 

Recently uploaded (20)

Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdf
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Formation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disksFormation of low mass protostars and their circumstellar disks
Formation of low mass protostars and their circumstellar disks
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.ppt
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 
Types of different blotting techniques.pptx
Types of different blotting techniques.pptxTypes of different blotting techniques.pptx
Types of different blotting techniques.pptx
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptxPhysiochemical properties of nanomaterials and its nanotoxicity.pptx
Physiochemical properties of nanomaterials and its nanotoxicity.pptx
 

Topic 3

  • 1. Inner And Nested Classes Bilal H. Rasheed MSc.(C.S) Nov. 2019
  • 2. Nested classes • Nested class is a class defined inside a class, that can be used within the scope of the class in which it is defined. In C++ nested classes are not given importance because of the strong and flexible usage of inheritance. Its objects are accessed using "Nest::Display".
  • 3. Example: • #include <iostream.h> using name space std(); class Nest { public: class Display { private: int s; public: void sum( int a, int b) { s =a+b; } void show( ) { cout << "nSum of a and b is:: " << s;} }; }; void main() { Nest::Display x; x.sum(12, 10); x.show(); }
  • 4. Result: • Sum of a and b is::22 In the above example, the nested class "Display" is given as "public" member of the class "Nest".
  • 5. Local classes in C++ • Local class is a class defined inside a function. Following are some of the rules for using these classes. • Global variables declared above the function can be used with the scope operator "::". • Static variables declared inside the function can also be used. • Automatic local variables cannot be used. • It cannot have static data member objects. • Member functions must be defined inside the local classes. • Enclosing functions cannot access the private member objects of a local class.
  • 6. • A class declared inside a function becomes local to that function and is called Local Class in C++. For example, in the following program, Test is a local class in fun(). #include<iostream> • using namespace std; • • void fun() • { • class Test // local to fun • { • /* members of Test class */ • }; • } • • int main() • { • return 0; • } •
  • 7. Following are some interesting facts about local classes. 1) A local class type name can only be used in the enclosing function. For example, in the following program, declarations of t and tp are valid in fun(), but invalid in main(). • #include<iostream> • using namespace std; • • void fun() • { • // Local class • class Test • { • /* ... */ • }; • • Test t; // Fine • Test *tp; // Fine • } • • int main() • { • Test t; // Error • Test *tp; // Error • return 0; • }
  • 8. 2) All the methods of Local classes must be defined inside the class only. For example, program 1 works fine and program 2 fails in compilation.• // PROGRAM 1 • #include<iostream> • using namespace std; • • void fun() • { • class Test // local to fun • { • public: • • // Fine as the method is defined inside the local class • void method() { • cout << "Local Class method() called"; • } • }; • • Test t; • t.method(); • } • • int main() • { • fun(); • return 0; • } • Output: • Local Class method() called
  • 9. • // PROGRAM 2 • #include<iostream> • using namespace std; • • void fun() • { • class Test // local to fun • { • public: • void method(); • }; • • // Error as the method is defined outside the local class • void Test::method() • { • cout << "Local Class method()"; • } • } • • int main() • { • return 0; • } • Output: • Compiler Error: In function 'void fun()': error: a function-definition is not allowed here before '{' token
  • 10. 3) A Local class cannot contain static data members. It may contain static functions though. For example, program 1 fails in compilation, but program 2 works fine. • // PROGRAM 1 • #include<iostream> • using namespace std; • • void fun() • { • class Test // local to fun • { • static int i; • }; • } • • int main() • { • return 0; • } • Compiler Error: In function 'void fun()': error: local class 'class fun()::Test' shall not have static data member 'int fun()::Test :: program will given …
  • 11. • // PROGRAM 2 • #include<iostream> • using namespace std; • • void fun() • { • class Test // local to fun • { • public: • static void method() • { • cout << "Local Class method() called"; • } • }; • • Test::method(); • } • • int main() • { • fun(); • return 0; • } • Output: • Local Class method() called
  • 12. 4) Member methods of local class can only access static and enum variables of the enclosing function. Non-static variables of the enclosing function are not accessible inside local classes. For example, the program 1 compiles and runs fine. But, program 2 fails in compilation. • // PROGRAM 1 • #include<iostream> • using namespace std; • • void fun() • { • static int x; • enum {i = 1, j = 2}; • • // Local class • class Test • { • public: • void method() { • cout << "x = " << x << endl; // fine as x is static • cout << "i = " << i << endl; // fine as i is enum • } • }; • • Test t; • t.method(); • } • • int main() • { • fun(); • return 0; • } • Output: • x = 0 i = 1
  • 13. • // PROGRAM 2 • #include<iostream> • using namespace std; • • void fun() • { • int x; • • // Local class • class Test • { • public: • void method() { • cout << "x = " << x << endl; • } • }; • • Test t; • t.method(); • } • • int main() • { • fun(); • return 0; • } • Output: • In member function 'void fun()::Test::method()': error: use of 'auto' variable from containing function
  • 14. 5) Local classes can access global types, variables and functions. Also, local classes can access other local classes of same function.. For example, following program works fine. • #include<iostream> • using namespace std; • • int x; • • void fun() • { • // First Local class • class Test1 { • public: • Test1() { cout << "Test1::Test1()" << endl; } • }; • • // Second Local class • class Test2 • { • // Fine: A local class can use other local classes of same function • Test1 t1; • public: • void method() { • // Fine: Local class member methods can access global variables. • cout << "x = " << x << endl; • } • }; • • Test2 t; • t.method(); • }
  • 15. • int main() • { • fun(); • return 0; • } • Output: • Test1::Test1() x = 0
  • 16. Example: • #include <iostream.h> using name space std(); int y; void g(); int main() { g(); return 0; } void g() { class local { public: void put( int n) {::y=n;} int get() {return ::y;} }ab; ab.put(9.5); cout << "The value assigned to y is::"<< ab.get(); };
  • 17. Result: • The value assigned to y is::9 In the above example, the local class "local" uses the variable "y" which is declared globally. Inside the function it is used using the "::" operator. The object "ab" is used to set, get the assigned values in the local class.