+++++Абстракция данных
++ Создайте класс с именем Книга. Пусть он содержит два разных конструктора
Download 57.24 Kb.
|
dasturlash
- Bu sahifa navigatsiya:
- ++ Создайте класс с именем Книга
++ Создайте класс с именем Книга. Пусть он содержит два разных конструктора
#include #include class Book { public: Book() { title = ""; author = ""; year = 0; } Book(std::string bookTitle, std::string bookAuthor) { title = bookTitle; author = bookAuthor; year = 0; } Book(std::string bookTitle, std::string bookAuthor, int bookYear) { title = bookTitle; author = bookAuthor; year = bookYear; } std::string getTitle() { return title; } std::string getAuthor() { return author; } private: std::string title; std::string author; int year; }; int main() { Book defaultBook; Book titleAuthorBook("The Great Gatsby", "F. Scott Fitzgerald"); Book fullBook("The Catcher in the Rye", "J.D. Salinger", 1951); std::string defaultTitle = defaultBook.getTitle(); std::string defaultAuthor = defaultBook.getAuthor(); std::string titleAuthorTitle = titleAuthorBook.getTitle(); std::string titleAuthorAuthor = titleAuthorBook.getAuthor(); std::string fullTitle = fullBook.getTitle(); std::string fullAuthor = fullBook.getAuthor(); std::cout << "Default Book's Title: " << defaultTitle << ", Author: " << defaultAuthor << std::endl; std::cout << "Title and Author Book's Title: " << titleAuthorTitle << ", Author: " << titleAuthorAuthor << std::endl; std::cout << "Full Book's Title: " << fullTitle << ", Author: " << fullAuthor << std::endl; return 0; } ++ Создайте класс с именем Книга. Пусть он содержит два разных конструктора. . Используйте указатель this внутри класса #include #include class Book { public: Book() { this->title = ""; this->author = ""; this->year = 0; } Book(std::string title, std::string author) { this->title = title; this->author = author; this->year = 0; } Book(std::string title, std::string author, int year) { this->title = title; this->author = author; this->year = year; } std::string getTitle() { return this->title; } std::string getAuthor() { return this->author; } private: std::string title; std::string author; int year; }; int main() { Book defaultBook; Book titleAuthorBook("The Great Gatsby", "F. Scott Fitzgerald"); Book fullBook("The Catcher in the Rye", "J.D. Salinger", 1951); std::string defaultTitle = defaultBook.getTitle(); std::string defaultAuthor = defaultBook.getAuthor(); std::string titleAuthorTitle = titleAuthorBook.getTitle(); std::string titleAuthorAuthor = titleAuthorBook.getAuthor(); std::string fullTitle = fullBook.getTitle(); std::string fullAuthor = fullBook.getAuthor(); std::cout << "Default Book's Title: " << defaultTitle << ", Author: " << defaultAuthor << std::endl; std::cout << "Title and Author Book's Title: " << titleAuthorTitle << ", Author: " << titleAuthorAuthor << std::endl; std::cout << "Full Book's Title: " << fullTitle << ", Author: " << fullAuthor << std::endl; return 0; } Download 57.24 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling