0% found this document useful (0 votes)
5 views

05LocalClassAndContainerClassInCpp

Uploaded by

nanigir651
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

05LocalClassAndContainerClassInCpp

Uploaded by

nanigir651
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SHRI SHANKARACHARYA TECHNICAL CAMPUS

Local Class in C++


A class declared inside a function is known as a local class in C++ as it is local to that function.

An example of a local class is given as follows.

#include<iostream>

using namespace std;

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.

A program that demonstrates a local class in C++ is given as follows.

#include<iostream>

using namespace std;

void func() {

class LocalClass {

private:

int num;

public:

void getdata( int n) {

num = n;

void putdata() {

cout<<"The number is "<<num;

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 1


SHRI SHANKARACHARYA TECHNICAL CAMPUS

};

LocalClass obj;

obj.getdata(7);

obj.putdata();

int main() {

cout<<"Demonstration of a local class"<<endl;

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>

using namespace std;

class first {

public:

first(){

cout << "Hello from first class\n";

};

//container class

class second {

first f;

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 2


SHRI SHANKARACHARYA TECHNICAL CAMPUS

public:

//constructor

second(){

cout << "Hello from second class\n";

};

int main(){

second s;

return 0;

Container class with constructor.

class ContainerClass {

private:

OtherClass* otherObject;

public:

ContainerClass() {

otherObject = new OtherClass();

~ContainerClass() {

delete otherObject;

};

Example with pointer and Constructor


#include <iostream>

using namespace std;

class Point {

private:

int x, y ;

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 3


SHRI SHANKARACHARYA TECHNICAL CAMPUS

public:

Point (int x, int y) {

this -> x = x;

this -> y = y;

int getX() {

return x;

int getY() {

return y;

};

class Rectangle {

private:

Point* topLeft;

int width, height;

public:

Rectangle (int x, int y, int width, int height) {

topLeft = new Point (x, y);

this -> width = width;

this -> height = height;

~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 () {

Rectangle rect(10, 20, 30, 40);

Point* topLeft = rect.getTopLeft();

cout<< "Rectangle width: " <<rect.getWidth() <<endl;

cout<< "Rectangle height: " <<rect.getHeight() <<endl;

cout<< "Rectangle top-left corner coordinates: (" <<topLeft->getX() << ", " <<topLeft->getY() << ")"
<<endl;

return 0;

SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 5

You might also like