SlideShare a Scribd company logo
C++ Programming - 6th Study
C++ Programming - 6th Study
3
4
5
6
7
8
class Person
{
private:
double height;
double weight;
public:
Person() {
cout << "Person() Called!n";
}
};
int main()
{
Person p1;
Person* p2 = new Person();
}
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
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);
}
11
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);
}
C++ Programming - 6th Study
14
15
16
17
18
class IntPointer
{
private:
int* pi;
public:
IntPointer()
{
pi = new int[5];
}
~IntPointer()
{
delete[] pi;
pi = nullptr;
}
};
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;
}
C++ Programming - 6th Study
21
22
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;
}
24
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;
}
26

More Related Content

C++ Programming - 6th Study

  • 3. 3
  • 4. 4
  • 5. 5
  • 6. 6
  • 7. 7
  • 8. 8 class Person { private: double height; double weight; public: Person() { cout << "Person() Called!n"; } }; int main() { Person p1; Person* p2 = new Person(); }
  • 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); }
  • 11. 11
  • 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); }
  • 14. 14
  • 15. 15
  • 16. 16
  • 17. 17
  • 18. 18 class IntPointer { private: int* pi; public: IntPointer() { pi = new int[5]; } ~IntPointer() { delete[] pi; pi = nullptr; } };
  • 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; }
  • 21. 21
  • 22. 22
  • 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; }
  • 24. 24
  • 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; }
  • 26. 26