05LocalClassAndContainerClassInCpp
05LocalClassAndContainerClassInCpp
#include<iostream>
void func() {
class LocalClass {
};
int main() {
return 0;
In the above example, func() is a function and class LocalClass is defined inside the function. So, it is
known as a local class.
A local class name can only be used in its function and not outside it. Also, the methods of a local
class must be defined inside it only. A local class cannot have static data members but it can have
static functions.
#include<iostream>
void func() {
class LocalClass {
private:
int num;
public:
num = n;
void putdata() {
};
LocalClass obj;
obj.getdata(7);
obj.putdata();
int main() {
func();
return 0;
Containership in C++
The parameter if a certain class contains another class is called a containership. The inside class is
called the contained class, while the class in which it is present is called the container class.
Containership is essentially the practice of putting one object inside another. It is made possible by
using object pointers that enable the development of intricate relationships between objects..
When using the C++ containership syntax, a class is made up of a different class as one among its
member variables. To accomplish this, an object pointer is initialized in the constructor after being
declared as an internal variable of the class.
#include <iostream>
class first {
public:
first(){
};
//container class
class second {
first f;
public:
//constructor
second(){
};
int main(){
second s;
return 0;
class ContainerClass {
private:
OtherClass* otherObject;
public:
ContainerClass() {
~ContainerClass() {
delete otherObject;
};
class Point {
private:
int x, y ;
public:
this -> x = x;
this -> y = y;
int getX() {
return x;
int getY() {
return y;
};
class Rectangle {
private:
Point* topLeft;
public:
~Rectangle() {
delete topLeft;
}
SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 4
SHRI SHANKARACHARYA TECHNICAL CAMPUS
int getWidth() {
return width;
int getHeight() {
return height;
Point* getTopLeft() {
return topLeft;
};
int main () {
cout<< "Rectangle top-left corner coordinates: (" <<topLeft->getX() << ", " <<topLeft->getY() << ")"
<<endl;
return 0;