Start here:
#include <iostream> #include <string> #include <sstream> using namespace std; class SimpleBook { public: SimpleBook(); SimpleBook(string aName, string aIdentifier, string text = ""); string ToString() const; string GetName() const; string GetIdentifier() const; string GetText() const; void SetName(string aName); void SetIdentifier(string aIdentifier); void SetText(string aText); string Read() const; private: string bookName; string bookIdentifier; string bookText; }; SimpleBook::SimpleBook() { bookName = "none"; bookIdentifier = "none"; bookText = "none"; } SimpleBook::SimpleBook(string aName, string aIdentifier, string aText) { bookName = aName; bookIdentifier = aIdentifier; bookText = aText; } string SimpleBook::ToString() const { ostringstream aStream; aStream << "Title: " << bookName << " Identifier: " << bookIdentifier << " Text: " << bookText; return aStream.str(); } string SimpleBook::GetName() const { return bookName; } string SimpleBook::GetIdentifier() const{ return bookIdentifier; } string SimpleBook::GetText() const { return bookText; } void SimpleBook::SetName(string aName) { bookName = aName; } void SimpleBook::SetIdentifier(string aIdentifier) { bookIdentifier = aIdentifier; } void SimpleBook::SetText(string aText) { bookText = aText; } string SimpleBook::Read() const { return bookText; } int main() { SimpleBook aBook; SimpleBook otherBook("Book of Mormon","AA 1","I Nephi having.."); cout << otherBook.ToString() << endl; cout << "Read some books:" << endl; cout << "From: " << otherBook.GetIdentifier() << ": " << otherBook.Read() << endl; cout << "From: " << aBook.GetIdentifier() << ": " << aBook.Read() << endl; system("pause"); return 0; }
Here is an example with inheritance
#include <iostream> #include <string> #include <sstream> using namespace std; class SimpleBook { public: SimpleBook(string aName, string aIdentifier, string text = ""); string ToString() const; string GetName() const; string GetIdentifier() const; string GetText() const; void SetName(string aName); void SetIdentifier(string aIdentifier); void SetText(string aText); string Read() const; protected: string bookName; string bookIdentifier; string bookText; }; SimpleBook::SimpleBook(string aName, string aIdentifier, string aText) { bookName = aName; bookIdentifier = aIdentifier; bookText = aText; } string SimpleBook::ToString() const { ostringstream aStream; aStream << "Title: " << bookName << " Identifier: " << bookIdentifier << " Text: " << bookText; return aStream.str(); } string SimpleBook::GetName() const { return bookName; } string SimpleBook::GetIdentifier() const{ return bookIdentifier; } string SimpleBook::GetText() const { return bookText; } void SimpleBook::SetName(string aName) { bookName = aName; } void SimpleBook::SetIdentifier(string aIdentifier) { bookIdentifier = aIdentifier; } void SimpleBook::SetText(string aText) { bookText = aText; } string SimpleBook::Read() const { return bookText; } class PublishedBook : public SimpleBook { public: PublishedBook(string aName, string aIdentifier, string text = "", string publisher = "Self Published"); string ToString() const; string GetPublisher() const; void SetPublisher(string aName); protected: string publisher; }; PublishedBook::PublishedBook(string aName, string aIdentifier, string aText, string aPublisher) : SimpleBook(aName, aIdentifier, aText), publisher(aPublisher) { } string PublishedBook::ToString() const { ostringstream aStream; aStream << SimpleBook::ToString() << " Publisher: " << publisher; return aStream.str(); } string PublishedBook::GetPublisher() const { return publisher; } void PublishedBook::SetPublisher(string aPublisher) { publisher = aPublisher; } class Manuscript : public SimpleBook { public: Manuscript(string aName, string aIdentifier, string text = "", int age = 0); string ToString() const; int GetAge() const; void SetAge(int); protected: int age; }; Manuscript::Manuscript(string aName, string aIdentifier, string aText, int anAge) : SimpleBook(aName, aIdentifier, aText), age(anAge) { } string Manuscript::ToString() const { ostringstream aStream; aStream << SimpleBook::ToString() << " Age: " << age; return aStream.str(); } int Manuscript::GetAge() const { return age; } void Manuscript::SetAge(int anAge) { age = anAge; } int main() { SimpleBook aBook("A book", "AA 1", "I don't write much .."); PublishedBook otherBook("Book of Mormon", "AA 2", "I Nephi having..", "Grandin"); Manuscript talmage("Class notes from Talmage", "AA 3", "Day one: we studied...", 57); cout << aBook.ToString() << endl; cout << otherBook.ToString() << endl; cout << talmage.ToString() << endl; cout << "Read some books:" << endl; cout << "From: " << otherBook.GetIdentifier() << ": " << otherBook.Read() << endl; cout << "From: " << aBook.GetIdentifier() << ": " << aBook.Read() << endl; system("pause"); return 0; }Back to top