Загрузка данных


#include <iostream>
#include <string>
using namespace std;

class GeomBase {
protected:
    int x0{ 0 }, y0{ 0 }, x1{ 0 }, y1{ 0 };
public:
    void set_coords(int x0, int y0, int x1, int y1)
    {
        this->x0 = x0;
        this->y0 = y0;
        this->x1 = x1;
        this->y1 = y1;
    }

    virtual void draw() const
    {
        cout << "GeomBase" << x0 << " " << " " << y0 << " " x1 << " " y1 << endl;
    }
};

class Line : public GeomBase {
private:
    double length{ 0.0 };
public:
    virtual void draw() const override
    {
        cout << "GeomBase" << x0 << " " << " " << y0 << " " x1 << " " y1 << endl;
    }
};

class Rect : public GeomBase {
public:
    virtual void draw() const override
    {
        printf("Rect: %d, %d, %d, %d\n", x0, y0, x1, y1);
    }
};

class Ellipse : public GeomBase {
public:
    virtual void draw() const override
    {
        printf("Ellipse: %d, %d, %d, %d\n", x0, y0, x1, y1);
    }
};

class Circle : public GeomBase {
public:
    virtual void draw() const override
    {
        printf("Circle: %d, %d, %d, %d\n", x0, y0, x1, y1);
    }
};

int main()
{
    GeomBase* g1 = new Line;
    GeomBase* g2 = new Rect;
    GeomBase* g3 = new Line;
    GeomBase* g4 = new Ellipse;

    delete g1;
    delete g2;
    delete g3;
    delete g4;

    return 0;
}






// C++ Program to Implement Interfaces Using Abstract Class

#include <iostream>
#include <string>
using namespace std;

// Declare the Student interface as an abstract class
class Student {
public:
    // Pure virtual function to get student information
    virtual string getStudentInfo() = 0;
    // Pure virtual function to print student information
    virtual void printStudentInfo() = 0;
};

// Define the UndergraduateStudent class that inherits from
// Student
class UndergraduateStudent : public Student {
private:
    string name;
    int year;
    string major;

public:
    // Constructor to initialize member variables
    UndergraduateStudent(string n, int y, string m)
        : name(n)
        , year(y)
        , major(m)
    {
    }

    // Override the getStudentInfo function to return
    // formatted student info
    string getStudentInfo() override
    {
        return name + ", Year: " + to_string(year)
            + ", Major: " + major;
    }

    // Override the printStudentInfo function to print
    // student info to console
    void printStudentInfo() override
    {
        cout << "Undergraduate Student: "
            << getStudentInfo() << endl;
    }
};

// Define the GraduateStudent class that inherits from
// Student
class GraduateStudent : public Student {
private:
    string name;
    string program;
    string thesis;

public:
    // Constructor to initialize member variables
    GraduateStudent(string n, string p, string t)
        : name(n)
        , program(p)
        , thesis(t)
    {
    }

    // Override the getStudentInfo function to return
    // formatted student info
    string getStudentInfo() override
    {
        return name + ", Program: " + program
            + ", Thesis: " + thesis;
    }

    // Override the printStudentInfo function to print
    // student info to console
    void printStudentInfo() override
    {
        cout << "Graduate Student: " << getStudentInfo()
            << endl;
    }
};

// Driver Code
int main()
{
    // Create an instance of UndergraduateStudent and print
    // its info
    UndergraduateStudent undergrad("Mohit Kumar", 3,
        "Computer Science");
    undergrad.printStudentInfo();

    // Create an instance of GraduateStudent and print its
    // info
    GraduateStudent grad("Rohit Kumar", "Master of Science",
        "AI");
    grad.printStudentInfo();