-1
#include "iostream"
using namespace std;
class Algebra
{
private:
int a, b;
const int c;
const int d;
static int s;
public:
//default constructor
Algebra() : c(0), d(0)
{
s++;
a = b = 0;
cout << "Default Constructor" << endl;
}
//parameterized (overloaded) constructor
Algebra(int a, int b, int c1, int d1) : c(c1), d(d1)
{
s++;
setA(a);
setB(b);
cout << "Parameterized Constructor" << endl;
}
//copy (overloaded) constructor
Algebra(const Algebra &obj) : c(obj.c), d(obj.d)
{
s++;
this->a = obj.a;
this->b = obj.b;
cout << "Copy Constructor" << endl;
}
//Destructor
~Algebra()
{
s--;
cout << "Destructor Called" << endl;
}
//Setter for static member s
static void setS(int s)
{
Algebra::s = s;
}
//Getter for static member s
static int getS()
{
return s;
}
//Getter for constant data member c
int getC() const
{
return this->c;
}
//Setter for data member a
void setA(int a)
{
if(a < 0)
this->a = 0;
else
this->a = a;
}
//Setter for data member b
void setB(int b)
{
if(b < 0)
this->b = 0;
else
this->b = b;
}
};
int Algebra::s = 90;
int main()
{
Algebra obj1, obj2(1, 2, 3,4), obj3(obj1);
cout << "Size of object = " << sizeof(obj1) << endl;
return 0;
}
爲什麼sizeof運算符顯示大小爲16,其中我已經聲明瞭int類型的5個數據成員,它應該將所有5個數據成員相加,並將結果設爲20.我還檢查了sizeof運算符的靜態int單獨變量,工作正常。爲什麼sizeof運算符不顯示大小爲20?
您可以將其視爲一個全局對象,並在名稱空間(該類的名稱空間)內具有訪問限制。 –
感謝您的支持 – Junaid
@JasonR:再次回答這個問題屬於_answer_部分。它在下面。 –