C++ Programming - 6th Study
- 9. class Person {
private:
double height;
double weight;
public:
Person(double _height, double _weight)
{
height = _height;
weight = _weight;
}
};
int main() {
Person p1(183.4, 78.5);
Person p2; // Error!
}
9
- 10. 10
class Person {
private:
const string SSN;
double height;
double weight;
public:
Person() { }
Person(const string _SSN, double _height, double _weight) {
SSN = _SSN;
height = _height;
weight = _weight;
}
};
int main() {
Person p("123456-1234567", 183.4, 78.5);
}
- 12. 12
class Person {
private:
const string SSN;
double height;
double weight;
public:
Person() { }
Person(const string _SSN, double _height, double _weight)
: SSN(_SSN), height(_height), weight(_weight)
{ }
};
int main() {
Person p("123456-1234567", 183.4, 78.5);
}
- 19. 19
class A {
public:
A() { cout << "A() Called!n"; }
~A() { cout << "~A() Called!n"; }
};
class B {
public:
B() { cout << "B() Called!n"; }
~B() { cout << "~B() Called!n"; }
};
int main() {
A a;
B b;
}
- 23. 23
class Person {
private:
double height;
double weight;
public:
Person() { }
Person(double _height, double _weight)
: height(_height), weight(_weight) { }
void setHeight(double height) { height = height; }
double getHeight() { return height; }
};
int main() {
Person p(183.4, 78.5);
p.setHeight(182.8);
cout << p.getHeight() << endl;
}
- 25. 25
class Person {
private:
double height;
double weight;
public:
Person() { }
Person(double _height, double _weight)
: height(_height), weight(_weight) { }
void setHeight(double height) {this->height = height; }
double getHeight() { return height; }
};
int main() {
Person p(183.4, 78.5);
p.setHeight(182.8);
cout << p.getHeight() << endl;
}