1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <iostream>
#include <string>
struct text {
text() = default ; // initialise using default member initialisers
text( const std::string& fnt, int sz, const std::string& clr, const std::string& dat )
: font(fnt), size(sz), colour(clr), data(dat) {}
void print() const {
std::cout << "Font: " << font << '\n'
<< "Size: " << size << '\n'
<< "Text Colour: " << colour << '\n'
<< "Data: " << data << '\n' ;
}
private:
std::string font = "Ariel" ;
int size = 10 ;
std::string colour = "Black";
std::string data = "Sample text" ;
};
struct box {
box() = default ; // initialise using default member initialisers
box( int wdth, int ht, const std::string& clr )
: width(wdth), height(ht), colour(clr) {}
void print() const {
std::cout << "Width: " << width << '\n'
<< "Height: " << height << '\n'
<< "Box Colour: " << colour << '\n' ;
}
private:
int width = 25 ;
int height = 20 ;
std::string colour = "Red" ;
};
struct text_box : public text, public box {
text_box() = default ; // default initialise base class sub-objects
// initialise (copy construct) base class sub-objects
text_box( const text& txt, const box& bx ) : text(txt), box(bx) {}
text_box& set( const text& txt, const box& bx ) {
// assign to base class sub-objects
text::operator= (txt) ;
box::operator= (bx) ;
return *this ;
}
void print() const {
std::cout << "----------\ntext_box\n---------\n" ; // print header
text::print() ; // print first base class stuff
box::print() ; // print second base class stuff
std::cout << "-------------------\n\n" ; // print footer
}
};
int main() {
text_box tb ; // default initialise
tb.print() ;
tb.set( text( "Baskerville", 22, "Magenta", "Hello World!" ),
box( 60, 30, "Blue" ) ) ;
tb.print() ;
}
|