+++++Абстракция данных


++ Создайте класс с именем Книга


Download 57.24 Kb.
bet16/23
Sana20.11.2023
Hajmi57.24 Kb.
#1790263
1   ...   12   13   14   15   16   17   18   19   ...   23
Bog'liq
dasturlash

++ Создайте класс с именем Книга. Объявляйте методы и свойства, используя
спецификаторы public и private. Используйте их в основной программ
#include
#include
class Book {
public:
Book()
title = "";
author = "";
year = 0;
price = 0.0;
}
Book(std::string bookTitle, std::string bookAuthor, int bookYear, double bookPrice) {
title = bookTitle;
author = bookAuthor;
year = bookYear;
price = bookPrice;
}
std::string getTitle() {
return title;
}
std::string getAuthor() {
return author;
}
private:
std::string title;
std::string author;
int year;
double price;
};
int main() {
Book defaultBook;
Book customBook("The Great Gatsby", "F. Scott Fitzgerald", 1925, 10.99);
std::string defaultTitle = defaultBook.getTitle();
std::string defaultAuthor = defaultBook.getAuthor();
std::string customTitle = customBook.getTitle();
std::string customAuthor = customBook.getAuthor();
std::cout << "Default Book's Title: " << defaultTitle << ", Author: " << defaultAuthor << std::endl;
std::cout << "Custom Book's Title: " << customTitle << ", Author: " << customAuthor << std::endl;
return 0;
}


++ Создайте класс с именем Книга. Пусть он содержит два метода и три свойства.
Отправить автора книги с помощью метода getAuthor()
#include
#include
class Book {
public:
Book() {
title = "";
author = "";
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 customBook("The Great Gatsby", "F. Scott Fitzgerald", 1925);
std::string defaultTitle = defaultBook.getTitle();
std::string defaultAuthor = defaultBook.getAuthor();
std::string customTitle = customBook.getTitle();
std::string customAuthor = customBook.getAuthor();
std::cout << "Default Book's Title: " << defaultTitle << ", Author: " << defaultAuthor << std::endl;
std::cout << "Custom Book's Title: " << customTitle << ", Author: " << customAuthor << std::endl;
return 0;
}

Download 57.24 Kb.

Do'stlaringiz bilan baham:
1   ...   12   13   14   15   16   17   18   19   ...   23




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling