Skip to content

Instantly share code, notes, and snippets.

@gcs-abdulwahab
Created October 30, 2023 12:21
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 gcs-abdulwahab/0de1b36623e488e271d124214092a71b to your computer and use it in GitHub Desktop.
Save gcs-abdulwahab/0de1b36623e488e271d124214092a71b to your computer and use it in GitHub Desktop.
nested Class
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
class Person {
class Address{
public:
string houseno;
string city;
Address(string houseno ="" , string city="lhr"){
this->houseno = houseno;
this->city = city;
}
string getAddress(){
return houseno +" " + city;
}
void setAddress(string hno , string city){
this->houseno = hno;
setCity(city);
}
void setCity(string city){
this->city = city;
}
string getCity(){
return city;
}
void print(){
cout<<" house no "<<houseno <<" city "<<city;
cout<< getAddress();
}
};
string name;
bool isMale;
Address currentAddress;
Address permanentAddress;
public:
Person(string name ){
this->name = name ;
}
void setCurrentAddress(string hno , string city){
currentAddress.houseno = hno;
currentAddress.city = city;
}
void setPermanentAddress(string hno , string city){
permanentAddress.houseno = hno;
permanentAddress.city = city;
}
void print(){
cout<<"name = "<<name<<endl;
cout<<"permanent address = "<<permanentAddress.getAddress() <<endl;
}
};
int main() {
Person p("abc" ) ;
p.setCurrentAddress("189", "Lahore" );
p.setPermanentAddress("1", "Lahore" );
p.print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment