Объектно-ориентированное программирование на C++
Изучение C++ на YouTube: ссылка на видео
Введение в классы и объекты
#include <iostream>
#include <list>
using namespace std;
class YouTubeChannel {
public:
string Name;
string OwnerName;
int SubscribersCount{
};
list<string> PublishedVideoTitles;
};
int main() {
YouTubeChannel ytChannel;
ytChannel.Name = "CodeBeauty";
ytChannel.OwnerName = "Saldina";
ytChannel.SubscribersCount = 1000;
ytChannel.PublishedVideoTitles = {
"C++ for beginner 1", "C++ for beginner 2"};
cout << "Name: " << ytChannel.Name << endl;
cout << "OwnerName: " << ytChannel.OwnerName << endl;
cout << "SubscribersCount: " << ytChannel.SubscribersCount << endl;
for (const string &title: ytChannel.PublishedVideoTitles) {
cout << title << endl;
}
}
Что такое конструктор и метод класса
#include <iostream>
#include <list>
using namespace std;
class YouTubeChannel {
public:
string Name;
string OwnerName;
int SubscribersCount{
};
list<string> PublishedVideoTitles;
YouTubeChannel(string name, string ownername) {
Name = name;
OwnerName = ownername;
SubscribersCount = 0;
}
void getInfo() {
cout << "Name: " << Name << endl;
cout << "OwnerName: " << OwnerName << endl;
cout << "SubscribersCount: " << SubscribersCount << endl;
for (const string &title: PublishedVideoTitles) {
cout << title << endl;
}
}
};
int main() {
YouTubeChannel ytChannel("CodeBeauty", "Saldina");
ytChannel.PublishedVideoTitles.push_back("C++ for beginner 1");
ytChannel.PublishedVideoTitles.push_back("C++ for beginner 2");
ytChannel.getInfo();
}
Что такое инкапсуляция в программировании
#include <iostream>
#include <list>
using namespace std;
class YouTubeChannel {
private:
string Name;
string OwnerName;
int SubscribersCount;
list<string> PublishedVideoTitles;
public:
YouTubeChannel(string name, string ownername) {
Name = name;
OwnerName = ownername;
SubscribersCount = 0;
}
void getInfo() {
cout << "Name: " << Name << endl;
cout << "OwnerName: " << OwnerName << endl;
cout << "SubscribersCount: " << SubscribersCount << endl;
for (const string &title: PublishedVideoTitles) {
cout << title << endl;
}
}
void unSubscribe() {
if (SubscribersCount > 0) {
SubscribersCount--;
}
}
void publishVideo(const string& title) {
PublishedVideoTitles.push_back(title);
}
};
int main() {
YouTubeChannel ytChannel("CodeBeauty", "Saldina");
ytChannel.publishVideo("C++ for beginner 1");
ytChannel.publishVideo("C++ for beginner 2");
ytChannel.unSubscribe();
ytChannel.getInfo();
}
Что такое наследование
#include <iostream>
#include <list>
using namespace std;
class YouTubeChannel {
private:
string Name;
int SubscribersCount;
list<string> PublishedVideoTitles;
protected:
string OwnerName;
public:
YouTubeChannel(string name, string ownername) {
Name = name;
OwnerName = ownername;
SubscribersCount = 0;
}
void getInfo() {
cout << "Name: " << Name << endl;
cout << "OwnerName: " << OwnerName << endl;
cout << "SubscribersCount: " << SubscribersCount << endl;
for (const string &title: PublishedVideoTitles) {
cout << title << endl;
}
}
void subscribe() {
SubscribersCount++;
}
void unSubscribe() {
if (SubscribersCount > 0) {
SubscribersCount--;
}
}
void publishVideo(const string &title) {
PublishedVideoTitles.push_back(title);
}
};
class CookingChannel : public YouTubeChannel {
public:
CookingChannel(string name, string ownername) : YouTubeChannel(name, ownername) {
}
void practice() {
cout << OwnerName << " is practicing cooking, learning new recipes." << endl;
}
};
int main() {
CookingChannel cookingChannel("Amy's kitchen", "Amy");
cookingChannel.publishVideo("apple pie");
cookingChannel.publishVideo("chocolate cake");
cookingChannel.subscribe();
cookingChannel.subscribe();
cookingChannel.practice();
cookingChannel.getInfo();
}
Что такое полиморфизм
#include <iostream>
#include <list>
using namespace std;
class YouTubeChannel {
private:
string Name;
int SubscribersCount;
list<string> PublishedVideoTitles;
protected:
string OwnerName;
int contentQuality;
public:
YouTubeChannel(string name, string ownername) {
Name = name;
OwnerName = ownername;
SubscribersCount = 0;
contentQuality = 0;
}
void getInfo() {
cout << "Name: " << Name << endl;
cout << "OwnerName: " << OwnerName << endl;
cout << "SubscribersCount: " << SubscribersCount << endl;
for (const string &title: PublishedVideoTitles) {
cout << title << endl;
}
}
void subscribe() {
SubscribersCount++;
}
void unSubscribe() {
if (SubscribersCount > 0) {
SubscribersCount--;
}
}
void publishVideo(const string &title) {
PublishedVideoTitles.push_back(title);
}
void checkAnalytics() {
if (contentQuality < 3) {
cout << Name << " has bad quality content." << endl;
} else {
cout << Name << " has good quality content." << endl;
}
}
};
class CookingChannel : public YouTubeChannel {
public:
CookingChannel(string name, string ownername) : YouTubeChannel(name, ownername) {
}
void practice() {
cout << OwnerName << " is practicing cooking, learning new recipes." << endl;
contentQuality++;
}
};
class SingerChannel : public YouTubeChannel {
public:
SingerChannel(string name, string ownername) : YouTubeChannel(name, ownername) {
}
void practice() {
cout << OwnerName << " is practicing singing, learning new songs." << endl;
contentQuality++;
}
};
int main() {
CookingChannel cookingChannel("Amy's kitchen", "Amy");
cookingChannel.practice();
SingerChannel singChannel("JohnSings", "John");
singChannel.practice();
singChannel.practice();
singChannel.practice();
singChannel.practice();
YouTubeChannel *yt1 = &cookingChannel;
YouTubeChannel *yt2 = &singChannel;
yt1->checkAnalytics();
yt2->checkAnalytics();
}
Виртуальные функции
#include <iostream>
using namespace std;
class Instrument {
public:
virtual void makeSound() {
cout << "Instrument playing...\n";
}
};
class Piano : public Instrument {
public:
void makeSound() {
cout << "Piano playing...\n";
}
};
int main() {
Instrument i;
i.makeSound(); // Instrument playing...
Instrument *ptrI = new Piano();
ptrI->makeSound(); // Piano playing...
}
Чисто виртуальные функции
#include <iostream>
using namespace std;
class Instrument {
public:
virtual void makeSound() = 0;
};
class Accordion : public Instrument {
public:
void makeSound() {
cout << "Accordion playing...\n";
}
};
class Piano : public Instrument {
public:
void makeSound() {
cout << "Piano playing...\n";
}
};
int main() {
// Instrument i; // Variable type 'Instrument' is an abstract class
Instrument *ptrI2 = new Accordion();
ptrI2->makeSound(); // Accordion playing...
Instrument *ptrI1 = new Piano();
ptrI1->makeSound(); // Piano playing...
Instrument *instruments[2] = {
ptrI1, ptrI2};
for (auto &instrument: instruments) {
instrument->makeSound();
}
}
Объяснение абстрактных классов в ООП
#include <iostream>
using namespace std;
class Smartphone {
public:
virtual void takeASelfie() = 0;
virtual void makeACall() = 0;
};
class Android : public Smartphone {
public:
void takeASelfie() override {
cout << "Android selfie\n";
}
void makeACall() override {
cout << "Android calling\n";
}
};
class iOS : public Smartphone {
public:
void takeASelfie() override {
cout << "iOS selfie\n";
}
void makeACall() override {
cout << "iOS calling\n";
}
};
int main() {
Smartphone *s1 = new Android();
s1->takeASelfie();
s1->makeACall();
Smartphone *s2 = new iOS();
s2->takeASelfie();
s2->makeACall();
}
Перегрузка оператора
#include <iostream>
#include <list>
#include <utility>
using namespace std;
struct YouTubeChannel {
string Name;
int SubscribersCount;
YouTubeChannel(string name, int subscribersCount) {
Name = std::move(name);
SubscribersCount = subscribersCount;
}
bool operator==(const YouTubeChannel &channel) const {
return this->Name == channel.Name;
}
};
ostream &operator<<(ostream &COUT, YouTubeChannel &youTubeChannel) {
COUT << "name: " << youTubeChannel.Name << endl;
COUT << "SubscribersCount: " << youTubeChannel.SubscribersCount << endl;
return COUT;
}
struct MyCollection {
list<YouTubeChannel> myChannels;
void operator+=(const YouTubeChannel &channel) {
this->myChannels.push_back(channel);
}
void operator-=(const YouTubeChannel &channel) {
this->myChannels.remove(channel); // must implement `operator==` in `YouTubeChannel`
}
};
ostream &operator<<(ostream &COUT, MyCollection &myCollection) {
for (YouTubeChannel ytChannel: myCollection.myChannels) {
COUT << ytChannel << endl;
}
return COUT;
}
int main() {
YouTubeChannel yt1 = YouTubeChannel("CodeBeauty", 2000);
YouTubeChannel yt2 = YouTubeChannel("CodeHandsome", 1000);
// cout << yt1 << yt2;
// operator<<(cout, yt1);
MyCollection myCollection;
myCollection += yt1;
myCollection += yt2;
myCollection -= yt2;
cout << myCollection << endl;
}
Дружественные функции и классы
#include <iostream>
using namespace std;
class EquilateralTriangle {
private:
float a;
float circumference;
double area;
public:
void setA(float length) {
a = length;
circumference = a * 3;
area = (1.73 * a * a) / 4;
}
friend void printResults(EquilateralTriangle);
friend class Homework;
};
void printResults(EquilateralTriangle et) {
cout << "circumference: " << et.circumference << endl;
cout << "area: " << et.area << endl;
}
class Homework {
public:
void m_printResults(EquilateralTriangle et) {
cout << "circumference: " << et.circumference << endl;
cout << "area: " << et.area << endl;
}
};
int main() {
EquilateralTriangle et{
};
et.setA(3);
printResults(et);
Homework hw;
hw.m_printResults(et);
}