Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ program to implement the initialization of Nested Members in a class:
#include<iostream>
using namespace std;
class first{
int fa,fb;
public:
first();
first(int x,int y);
void display();
};
first::first():fa(0),fb(0){
}
first::first(int x,int y):fa(x),fb(y){
}
void first::display(){
cout<<fa<<" "<<fb;
}
class second{
first f;
int sa,sb;
public:
second();
second(int x,int y,int a,int b);
void display();
};
second::second():f(),sa(0),sb(0){
}
second::second(int x,int y,int a,int b):f(x,y),sa(a),sb(b){
}
void second::display(){
cout<<"The Numbers are ";
f.display();
cout<<" "<<sa<<" "<<sb<<endl;
}
int main()
{
second obj;
cout<<"Initially,";
obj.display();
int a,b,c,d;
cout<<"Enter any four numbers.";
cin>>a>>b>>c>>d;
second obj2(a,b,c,d);
cout<<"After,";
obj2.display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.