Abstract Classes And Pure Virtual Functions | C++ Tutorial

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

How has this only got 3.9k views! This is a fantastic explanation, thank you :D

brandongill
Автор

Clear and concise explanation of the relationship between abstract classes and virtual functions. thank you:)

Mtlik
Автор

What is the purpouse of using pointers to classes (a.k.a objects) in your example (and also couple of previous videos) if you are not using anything pointer-related stuff such as pointer arithmetics ? Could you do all those examples with something like

Shape shapes [] = {Triangle(3, 4), Rectangle(5, 6)}

Can you also quickly elaborate what is the main adventage in general sense of dealing with object using pointers to instances, rather than object instances itself?
One more thing:
Does polymorphism work only with pointers so maybe you are using "Shape *shapes[]" on purpouse instead of "Shape shapes[]" ?

dzeno
Автор

You used the new keyword. How do i safely do that without memory leaks?

Blxxy
Автор

I did some modification to your program. I even tried using Smart Pointers but it didn't work.

#include <iostream>
#include <vector>
#include <string>
#include <math.h>

class Shape {
public:
virtual double area() = 0;
virtual const std::string name() const = 0;
};

class Square : public Shape {
public:
Square(double side) : m_side(side) {}
virtual double area() final { return pow(m_side, 2); }
virtual const std::string name() const { return "square"; }
~Square() {}
private:
double m_side;
};

class Triangle : public Shape {
public:
Triangle(double base, double height) : m_base(base), m_height(height) {}
virtual double area() final { return 0.5 * m_base * m_height; }
virtual const std::string name() const { return "triangle"; }
~Triangle() {}
private:
double m_base;
double m_height;
};

class Parallelogram : public Shape {
public:
Parallelogram(double side1, double side2) : m_side1(side1), m_side2(side2) {}
virtual double area() final { return m_side1 * m_side2; }
virtual const std::string name() const { return "parallelogram"; }
~Parallelogram() {}
private:
double m_side1;
double m_side2;
};

class Circle : public Shape {
public:
static double pi;
Circle(double radius) : m_radius(radius) {}
virtual double area() final { return pi * pow(m_radius, 2); }
virtual const std::string name() const { return "circle"; }
~Circle() {}
private:
double m_radius;
};

double Circle::pi = 3.1452;

int main()
{
Shape* shapes[4] = {
new Square(4),
new Triangle(6, 9),
new Parallelogram(11, 6),
new Circle(7)
};
for (int i{ 0 }; i < 4; ++i) {
std::cout << "Area of " << shapes[i]->name() << ": "
<< shapes[i]->area() << " sq units\n";
}
for (int i{ 0 }; i < 4; ++i) {
delete[] shapes[i];
}
return 0;
}

McDonaldIbekwe
Автор

Please, is there a way of doing it with Smart Pointers?

McDonaldIbekwe
Автор

I've watched many of your videos and the thoroughness of your explanations is wonderful. Awesome work!

MarcArnold-py
Автор

I really enjoyed watching your tutorials, concise and clear and good examples, Thanks a lot.

mosslnnx
Автор

Altough my english is bad. I understand purposes of abstract classes. In view of that situation I should say you are good teacher. Thanks

omercandemirci
Автор

Thank you sir, I've been struggling why do we need to instantiate a pointer to an abstract class, and now I got it.

mihaibozian
Автор

Uhm why am I getting this error? I don't understand why I am getting this at all, btw this error is apparently in the for loop cout command: "no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'void')"

animeIsle
Автор

Very Professional Explanation. Thank You So Much.

clammyclams
Автор

Yo kevin, its me once again:

its not a video-related question but, does template for switch case exist?
for example i got this:

case 1:
std::cout << "1 USD = 0, 93 EUR" << std::endl;
std::cout << "Now you got " << getBalance() * 0.93 << "EUR" << std::endl;
return;
case 2:
std::cout << "Error. You cant exchange USD to USD" << std::endl;
return;
case 3:
std::cout << "1 USD = 0, 95 CHF " << std::endl;
std::cout << "Now you got " << getBalance() * 0.95 << "CHF" << std::endl;
return;
case 4:
std::cout << "1 USD = 0, 77 GBP" << std::endl;
std::cout << "Now you got " << getBalance() * 0.77 << "GBP" << std::endl;
return;
default:
std::cout << "Invalid currency. Please select a valid one" << std::endl;


(the exchange proyect i was talking you about)

but x4 (it only changes the currency), so its kinda ambiguos doing the same thing 4 times. Is there any way i can implement a template ?

Vichained