Write a program to declare three classes a1, a2, a3. The classes have private data member variable of string type. Assign the first name and surname of a student to member variable of class a1 and a2 respectively. Perform concatenation of two strings and store it to data member variable of class a3. Print the full name to the output screen using member function.
#include<iostream>
using namespace std;
class a1{
private:
string first_name;
public:
void setFirst(string first){
first_name = first;
}
string getFirst(){
return first_name;
}
};
class a2{
private:
string surname;
public:
void setSurname(string first){
surname = first;
}
string getSurname(){
return surname;
}
};
class a3{
private:
a1 first;
a2 second;
public:
void setFull(string a, string b){
first.setFirst(a);
second.setSurname(b);
}
string getFull(){
return first.getFirst() +" "+second.getSurname();
}
};
int main(){
a3 third;
third.setFull("John", "Terry");
cout<<third.getFull()<<endl;
}
Comments
Leave a comment