salehvasaleh
Member
از دوستان عزیز خواهش می کنم هر کدوم چیزی در مورد توابع سازنده و مخرب در سی پلاس پلاس و کاربردهای اون می دونن بگن.ممنون می شم.
class [COLOR="Red"]Ratio[/COLOR]
{ public:
[COLOR="Blue"]Ratio[/COLOR](int n,int d) { num = n; den = d; }
void print() { cout << num << '/' << den; }
private:
int num,den;
};
int main()
{ Ratio x(-1,3), y(22,7);
cout << "x = ";
x.print();
cout << " and y = ";
y.print();
}
x = -1/3 and y = 22/7
class Ratio
{ public:
Ratio() { num = 0; den = 1; }
Ratio(int n) { num = n; den = 1; }
Ratio(int n,int d) { num = n; den = d; }
void print() { cout << num << '/' << den; }
private:
int num,den;
};
int main()
{ Ratio x,y(4),z(22 ,7);
cout << "x = ";
x.print();
cout << "\ny = ";
y.print();
cout << "\nz = ";
z.print();
}
x = 0/1
y = 4/1
z = 22/7
Ratio x,y(4),z(22 ,7
Ratio() { num = 0; den = 1; }
Ratio(int n) { num = n; den = 1; }
Ratio(int n,int d) { num = n; den = d; }
class Ratio
{ public:
Ratio(int n=0,int d=1) : num(n),den(d) { }
private:
int num,den;
};
int main()
{ Ratio x,y(4),z(22, 7);
}
class Ratio
{ public:
Ratio() { cout << "OBJECT IS BORN.\n"; }
~Ratio() { cout << "OBJECT DIES.\n"; }
private:
int num,den;
};
int main()
{ { Ratio x; // beginning of scope for x
cout << "Now x is alive.\n";
} // end of scope for x
cout << "Now between blocks.\n";
{ Ratio y;
cout << "Now y is alive.\n";
}
}
OBJECT IS BORN.
Now x is alive.
OBJECT DIES.
Now between blocks.
OBJECT IS BORN.
Now y is alive.
OBJECT DIES.
[COLOR="Blue"]#include <iostream.h>[/COLOR]
[COLOR="Blue"]#include <conio.h[/COLOR][COLOR="Blue"]>[/COLOR]
class Ratio
{ public:
Ratio() { cout << "OBJECT IS BORN.\n"; }
~Ratio() { cout << "OBJECT DIES.\n"; }
private:
int num,den;
};
int main()
{ { Ratio x; // beginning of scope for x
cout << "Now x is alive.\n";
} // end of scope for x
cout << "Now between blocks.\n";
{ Ratio y;
cout << "Now y is alive.\n";
}
[COLOR="Blue"]
getch();[/COLOR]
}