עבור לתוכן
View in the app

A better way to browse. Learn more.

HWzone

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

CPP- עזרה בתרגיל בסיסי של הורשה, עדכון- שאלה נוספת

Featured Replies

פורסם

שלום רב,

כתבתי אובייקט מסוג BigNumber_t שמכיל את הפרמטר char* data . לאובייקט שלושה בנאים ( ברירת מחדל, שמקבל מחרוזת, העתקה). אני צריך לכתוב בשלב הזה מחלקה BigInt_t שיורשת את המחלקה הנ"ל.

BigNumber_t.h


#ifndef BIGNUMBER_T
#define BIGNUMBER_T


#include<iostream>


using namespace std;


class BigNumber_t{
public:
BigNumber_t(){data = 0;}
BigNumber_t(const char*);
BigNumber_t(const BigNumber_t&);
~BigNumber_t();

void Print(){cout<<data<<endl;}




protected:
char* data;
};


#endif

BigNumber_t.cpp


#include"BigNumber_t.h"
#include<iostream>


using namespace std;




BigNumber_t::BigNumber_t(const char* number){
if (number!=0)
{
data = new char[strlen(number)+1];
strcpy(data,number);
}
else
data = 0;
}


BigNumber_t::BigNumber_t(const BigNumber_t& bigNum){
data = new char[strlen(bigNum.data)+1];
strcpy(data,bigNum.data);
}


BigNumber_t::~BigNumber_t(){
delete [] data;
data = 0;
}

BigInt_t.h


#ifndef BIGINT_T
#define BIGINT_T


#include"BigNumber_t.h"
#include<iostream>


using namespace std;


class BigInt_t : public BigNumber_t{



};


#endif

main.cpp


#include"BigNumber_t.h"
#include"BigInt_t.h"
#include<iostream>


using namespace std;




int main()
{
BigInt_t("123");
return 0;
}

וזו השגיאה שקופצת

error C2440: '<function-style-cast>' : cannot convert from 'const char [4]' to 'BigInt_t'

אני כנראה לא הבנתי משהו בנושא הורשה, אשמח אם מישהו יכוון אותי אל הטעות שלי

תודה

פורסם

קונסטרקטורים לא עוברים בירושה. אתה צריך להגדיר את הקונסטרקטור ל-BigInt_t במפורש, שיקרא לקונסטרקטור של האבא.

פורסם

ב-++C אין ירושה של בנאים (constructor). אתה צריך לממשם בעצמך.

ניתן לקרוא לבנאי המחלקה שירשת ממנה ב-initializer:


class Base
{
public:
Base(int x) : m_x( x ) { }
...
private:
int m_x;
};


class Derived : public Base
{
public:
Derived(int x) : Base(x) {}
...
}

פורסם
  • מחבר

תודה רבה, עובד

שאלה נוספת אני צריך לכתוב אופרטור+, אבל למשש אותו על ה base class אי אפשר כי זאת מחלקה כללית, אני צריך לממש אותו במחלקה BigInt_t ו BigFlt_t . המימוש של האופרטור קצת שונה בשתי המחלקות היורשות. . השאלה אם כזה דבר הוא חוקי?


virtual BigNumber_t& operator+(BigNumber_t&) = 0;

או שפשוט להכריז עליו בכל מחלקה יורשת ולממש אותו בקבצי ה cpp שלהן?

תודה

פורסם

באופן כללי, operator overloading ו-virtual לא תמיד עובדים טוב ביחד.

operator+ עובד על ערכים, והסמנטיקה שלו לא ממש מתאימה לקריאות וירטואליות.

הדבר הכי בטוח (safe) יהיה לממש אותו בנפרד בכל מחלקה.

פורסם
  • מחבר

תודה רבה :)

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.