Skip to content

Instantly share code, notes, and snippets.

@ravikiran0606
Last active July 31, 2016 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravikiran0606/d27ba4ed5be8ca8e255a0bdcc0edfff9 to your computer and use it in GitHub Desktop.
Save ravikiran0606/d27ba4ed5be8ca8e255a0bdcc0edfff9 to your computer and use it in GitHub Desktop.
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