Classes
Class Intro
A class groups related data and behavior into one type.
class
A class definition describes the fields and functions that objects of that type can use.
Class Intro
class_intro.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
class Rectangle {
public:
int width;
int height;
int area() {
return width * height;
}
};
int main() {
int width = 4;
int height = 3;
Rectangle card;
card.width = width;
card.height = height;
std::cout << "width=" << card.width << std::endl;
std::cout << "height=" << card.height << std::endl;
std::cout << "area=" << card.area() << std::endl;
return 0;
}
#include <iostream>
class Rectangle {
public:
int width;
int height;
int area() {
return width * height;
}
};
int main() {
int width = 2;
int height = 3;
Rectangle card;
card.width = width;
card.height = height;
std::cout << "width=" << card.width << std::endl;
std::cout << "height=" << card.height << std::endl;
std::cout << "area=" << card.area() << std::endl;
return 0;
}
#include <iostream>
class Rectangle {
public:
int width;
int height;
int area() {
return width * height;
}
};
int main() {
int width = 6;
int height = 3;
Rectangle card;
card.width = width;
card.height = height;
std::cout << "width=" << card.width << std::endl;
std::cout << "height=" << card.height << std::endl;
std::cout << "area=" << card.area() << std::endl;
return 0;
}
#include <iostream>
class Rectangle {
public:
int width;
int height;
int area() {
return width * height;
}
};
int main() {
int width = 4;
int height = 5;
Rectangle card;
card.width = width;
card.height = height;
std::cout << "width=" << card.width << std::endl;
std::cout << "height=" << card.height << std::endl;
std::cout << "area=" << card.area() << std::endl;
return 0;
}
#include <iostream>
class Rectangle {
public:
int width;
int height;
int area() {
return width * height;
}
};
int main() {
int width = 4;
int height = 7;
Rectangle card;
card.width = width;
card.height = height;
std::cout << "width=" << card.width << std::endl;
std::cout << "height=" << card.height << std::endl;
std::cout << "area=" << card.area() << std::endl;
return 0;
}
width ← 4, height ← 3, card ← (empty), card.width ← 4, card.height ← 3
13int main() {14 int width→ 4 = 4; //@width=2, 615 int height→ 3 = 3; //@height=5, 71617 Rectangle card→ (empty);18 card.width→ 4 = width4;19 card.height→ 3 = height3;2021 std::cout << "width=" << card(empty).width << std::endl;22 std::cout << "height=" << card(empty).height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;outputwidth=4 height=3int area()
8int area() {9 return width4 * height3;10}std::cout << "area=" << card.area() << std::endl;
22 std::cout << "height=" << card.height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;25}outputarea=12
width ← 2, height ← 3, card ← (empty), card.width ← 2, card.height ← 3
13int main() {14 int width→ 2 = 2;15 int height→ 3 = 3;1617 Rectangle card→ (empty);18 card.width→ 2 = width2;19 card.height→ 3 = height3;2021 std::cout << "width=" << card(empty).width << std::endl;22 std::cout << "height=" << card(empty).height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;outputwidth=2 height=3int area()
8int area() {9 return width2 * height3;10}std::cout << "area=" << card.area() << std::endl;
22 std::cout << "height=" << card.height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;25}outputarea=6
width ← 6, height ← 3, card ← (empty), card.width ← 6, card.height ← 3
13int main() {14 int width→ 6 = 6;15 int height→ 3 = 3;1617 Rectangle card→ (empty);18 card.width→ 6 = width6;19 card.height→ 3 = height3;2021 std::cout << "width=" << card(empty).width << std::endl;22 std::cout << "height=" << card(empty).height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;outputwidth=6 height=3int area()
8int area() {9 return width6 * height3;10}std::cout << "area=" << card.area() << std::endl;
22 std::cout << "height=" << card.height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;25}outputarea=18
width ← 4, height ← 5, card ← (empty), card.width ← 4, card.height ← 5
13int main() {14 int width→ 4 = 4;15 int height→ 5 = 5;1617 Rectangle card→ (empty);18 card.width→ 4 = width4;19 card.height→ 5 = height5;2021 std::cout << "width=" << card(empty).width << std::endl;22 std::cout << "height=" << card(empty).height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;outputwidth=4 height=5int area()
8int area() {9 return width4 * height5;10}std::cout << "area=" << card.area() << std::endl;
22 std::cout << "height=" << card.height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;25}outputarea=20
width ← 4, height ← 7, card ← (empty), card.width ← 4, card.height ← 7
13int main() {14 int width→ 4 = 4;15 int height→ 7 = 7;1617 Rectangle card→ (empty);18 card.width→ 4 = width4;19 card.height→ 7 = height7;2021 std::cout << "width=" << card(empty).width << std::endl;22 std::cout << "height=" << card(empty).height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;outputwidth=4 height=7int area()
8int area() {9 return width4 * height7;10}std::cout << "area=" << card.area() << std::endl;
22 std::cout << "height=" << card.height << std::endl;23 std::cout << "area=" << card(empty).area() << std::endl;24 return 0;25}outputarea=28