#include <iostream>
#include <stdio.h>
using namespace std;
// Base class
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
Shape()
{
printf("creating shape \n");
}
Shape(int h,int w)
{
height = h;
width = w;
printf("creatig shape with attributes\n");
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
Rectangle()
{
printf("creating rectangle \n");
}
Rectangle(int h,int w)
{
printf("creating rectangle with attributes \n");
height = h;
width = w;
}
};
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
Rectangle *square = new Rectangle(5,5);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
कार्यक्रम के उत्पादन में नीचेसी ++ बुला आधार वर्ग कंस्ट्रक्टर्स
creating shape
creating rectangle
creating shape
creating rectangle with attributes
Total area: 35
दिया जाता है का निर्माण दोनों व्युत्पन्न वर्ग वस्तुओं मैं देख रहा हूँ यह हमेशा डिफ़ॉल्ट आधार वर्ग निर्माता called.Is है इसके लिए एक कारण है? और क्या यही कारण है कि पाइथन जैसी भाषाएं सी ++ जैसी निहित कॉल की बजाय बेस क्लास कन्स्ट्रक्टर की स्पष्ट कॉल पर जोर देती हैं?
विरासत पदानुक्रम में प्रत्येक कन्स्ट्रक्टर को बेस -> व्युत्पन्न क्रम में बुलाया जाता है। विनाशकों को रिवर्स ऑर्डर में बुलाया जाता है। –
मेरा सवाल है "क्या यह हमेशा बेस क्लास का डिफ़ॉल्ट कन्स्ट्रक्टर है जिसे बुलाया जाता है?" – liv2hak
@ liv2hak लेकिन मुझे ऐसा लगता है कि 'आयताकार (int एच, int w) 'कन्स्ट्रक्टर दूसरे आयताकार init पर कॉल किया जाता है ... – Kupto