C++ program to implement the initialization of Nested Members in a class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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