G
G
Grigory Dikiy2015-04-03 20:05:18
OOP
Grigory Dikiy, 2015-04-03 20:05:18

Class inheritance?

Errors in class inheritance. Constructors are not inherited except for the default constructor.
main.h

#include <iostream>
#include <cstring>

class Stroka
{
    private:
        char *p_char; // Указатель на строку
        unsigned int size; // Длина строки
    public:
        /* Конструктор без параметров */
        Stroka()
        {
            size = 1;
            p_char = new char[size];
            *p_char = '\0';
            std::cout << "КОНСТРУКТОР  БЕЗ  ПАРАМЕТРОВ" << std::endl;
        }

        /* Конструктор с  параметрами */
        Stroka(const char *st)
        {
            size = strlen(st);
            p_char = new char[size];
            strcpy(p_char, st);
            std::cout << "КОНСТРУКТОР  С  ПАРАМЕТРАМИ" << std::endl;
        }

        /* Конструктор с  параметрами*/
        Stroka(const char ch)
        {
            size = 2;
            p_char = new char[size];
            p_char[0] =  ch;
            p_char[1] = '\0';
            std::cout << "КОНСТРУКТОР  С  ПАРАМЕТРАМИ КОТОРЫЙ ПРИНИМАЕТ СИМВОЛ" << std::endl;
        }

        /* Конструктор  копирования */
        Stroka(const Stroka &st)
        {
            size = strlen(st.p_char);
            p_char = new char[size];
            strcpy(p_char, st.p_char);
            std::cout << "КОНСТРУКТОР  С  КОПИРОВАНИЯ" << std::endl;
        }

        /* Вывод  строки на  экран */
        void Print()
        {
            std::cout << "ОТЛАДОЧНАЯ ПЕЧАТЬ\n" << p_char << std::endl;
        }

        /* Перегрузка  присваивания */
        Stroka operator=(const Stroka &st)
        {
            std::cout << "ПЕРЕГРУЗКА ОПЕРАЦИИ" << std::endl;
            delete [] p_char;
            p_char = new char[st.size];

            strcpy(p_char, st.p_char);
            return *this;
        }

        /* Длина  строки */
        unsigned int Size()
        {
            return strlen(p_char);
        }

        /* Деструктор */
        ~Stroka()
        {
            delete [] p_char;
            std::cout << "ДЕСТРУКТОР" << std::endl;
        }

};

class Str_Indef: public Stroka{};

using namespace std;

int main()
{
    Str_Indef str("test");

    str.Print();
    str.Size();
    return 0;
}

Compiler errors:
ceada8f30cf641eaa9f6c309c1f9ce7f.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2015-04-03
@frilix

class Str_Indef: public Stroka
{
public:
    using Stroka::Stroka;
}

Provided that you compile C++11. Before that, there was no constructor inheritance in C++ at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question