IPB
ЛогинПароль:

> Внимание!

1. Пользуйтесь тегами кода. - [code] ... [/code]
2. Точно указывайте язык, название и версию компилятора (интерпретатора).
3. Название темы должно быть информативным. В описании темы указываем язык!!!

 
 Ответить  Открыть новую тему 
> Множественное наследование, С++
Rocket
сообщение 12.04.2008 19:44
Сообщение #1


Знаток
****

Группа: Пользователи
Сообщений: 306
Пол: Мужской
Реальное имя: Евгений

Репутация: -  0  +


Вот текст программы:


#include<iostream>
#include<conio.h>

using namespace std;

class Base1
{ int i;
 
 public:
 Base1()
 { i=0;
   cout<<"Run Base1 Constructor!"<<endl;
        }
        
 Base1(int I)
 { i=I;
   cout<<"Run base1 ExtraConstructor!"<<endl;   
        }    
      
      };
      
class Base2
{ char M[20];
 
  public: 
  Base2()
  { 
    M = 'empty';
    cout<<"run Base2 Constructor!"<<endl;
         }
        
  Base2(char* m)
  { strcpy(M,m);
    cout<<"Run Base2 extraConstructor!"<<endl;
         }
      
      };
      
class Derived:public Base1, public Base2
{ char ch;

  friend ostream& operator << (ostream&, const Derived&);

  public:
  Derived()
  { ch="V";
    cout<<"Run Derived Constructor!"<<endl;
           }
    
   Derived(char CH, char* m, int I)
   {  ch=CH;
      strcpy(M,m);
      i=I;
      cout<<"Run Derived ExtraConstructor!"<<endl;
            }        
      
      };
      
ostream& operator << (ostream& os, const Derived& obj) 
 {
    os <<
        "ch = " << obj.ch <<endl <<"M = " << obj.M <<endl <<"i = " << obj.d <<endi;
     return os;
 }
 
 int main()
 {
     Derived Der;
     cout<<Der<<endl;
     }


Как инициализировать поле M словом 'empty' в конструкторе Base2()? Как реализовать интерфейсные функции в классах Base1, Base2, Derived, которые позволяли бы изменить или прочесть значения i, M, ch соответствующих классов?
 Оффлайн  Профиль  PM 
 К началу страницы 
+ Ответить 
volvo
сообщение 12.04.2008 20:26
Сообщение #2


Гость






Так?

#include<iostream>
#include<conio.h>

using namespace std;

class Base1 {
    int i;

public:
    int get_i() {
        return i;
    }
    Base1() {
        i=0;
        cout<<"Run Base1 Constructor!"<<endl;
    }

    Base1(int I) {
        i=I;
        cout<<"Run base1 ExtraConstructor!"<<endl;
    }
};

class Base2 {
    char M[20];

protected:


public:
    char *get_m() {
        return M;
    }

    Base2() {
        strcpy(M, "empty");
        cout<<"run Base2 Constructor!"<<endl;
    }

    Base2(char* m) {
        strcpy(M,m);
        cout<<"Run Base2 extraConstructor!"<<endl;
    }
};

class Derived:public Base1, public Base2 {
    char ch;

    friend ostream& operator << (ostream&, const Derived&);

public:
    Derived() {
        ch='V';
        cout<<"Run Derived Constructor!"<<endl;
    }

    Derived(char CH, char* m, int I):Base1(I), Base2(m) {
        ch=CH;
        cout<<"Run Derived ExtraConstructor!"<<endl;
    }
};

ostream& operator << (ostream& os, const Derived& obj)
{
    os <<
        "ch = " << obj.ch << endl <<
        "M = " << ((Base2)obj).get_m() << endl <<
        "i = " << ((Base1)obj).get_i() << endl;
    return os;
 }

int main() {
    Derived Der;
    cout<<Der<<endl;
}

 К началу страницы 
+ Ответить 
Rocket
сообщение 12.04.2008 21:13
Сообщение #3


Знаток
****

Группа: Пользователи
Сообщений: 306
Пол: Мужской
Реальное имя: Евгений

Репутация: -  0  +


Цитата(volvo @ 12.04.2008 21:26) *

Так?

#include<iostream>
#include<conio.h>

using namespace std;

class Base1 {
    int i;

public:
    int get_i() {
        return i;
    }
    Base1() {
        i=0;
        cout<<"Run Base1 Constructor!"<<endl;
    }

    Base1(int I) {
        i=I;
        cout<<"Run base1 ExtraConstructor!"<<endl;
    }
};

class Base2 {
    char M[20];

protected:
public:
    char *get_m() {
        return M;
    }

    Base2() {
        strcpy(M, "empty");
        cout<<"run Base2 Constructor!"<<endl;
    }

    Base2(char* m) {
        strcpy(M,m);
        cout<<"Run Base2 extraConstructor!"<<endl;
    }
};

class Derived:public Base1, public Base2 {
    char ch;

    friend ostream& operator << (ostream&, const Derived&);

public:
    Derived() {
        ch='V';
        cout<<"Run Derived Constructor!"<<endl;
    }

    Derived(char CH, char* m, int I):Base1(I), Base2(m) {
        ch=CH;
        cout<<"Run Derived ExtraConstructor!"<<endl;
    }
};

ostream& operator << (ostream& os, const Derived& obj)
{
    os <<
        "ch = " << obj.ch << endl <<
        "M = " << ((Base2)obj).get_m() << endl <<
        "i = " << ((Base1)obj).get_i() << endl;
    return os;
 }

int main() {
    Derived Der;
    cout<<Der<<endl;
}



О да, всё работает. Спасибо!
 Оффлайн  Профиль  PM 
 К началу страницы 
+ Ответить 

 Ответить  Открыть новую тему 
1 чел. читают эту тему (гостей: 1, скрытых пользователей: 0)
Пользователей: 0

 

- Текстовая версия 21.09.2025 12:02
Хостинг предоставлен компанией "Веб Сервис Центр" при поддержке компании "ДокЛаб"