📄 module10.lst
字号:
listing 1
// A simple class hierarchy.
#include <iostream>
#include <cstring>
using namespace std;
// A class for two-dimensional objects.
class TwoDShape {
public:
double width;
double height;
void showDim() {
cout << "Width and height are " <<
width << " and " << height << "\n";
}
};
// Triangle is derived from TwoDShape.
class Triangle : public TwoDShape {
public:
char style[20];
double area() {
return width * height / 2;
}
void showStyle() {
cout << "Triangle is " << style << "\n";
}
};
int main() {
Triangle t1;
Triangle t2;
t1.width = 4.0;
t1.height = 4.0;
strcpy(t1.style, "isosceles");
t2.width = 8.0;
t2.height = 12.0;
strcpy(t2.style, "right");
cout << "Info for t1:\n";
t1.showStyle();
t1.showDim();
cout << "Area is " << t1.area() << "\n";
cout << "\n";
cout << "Info for t2:\n";
t2.showStyle();
t2.showDim();
cout << "Area is " << t2.area() << "\n";
return 0;
}
listing 2
// A derived class of TwoDShape for rectangles.
class Rectangle : public TwoDShape {
public:
bool isSquare() {
if(width == height) return true;
return false;
}
double area() {
return width * height;
}
};
listing 3
// Access to private members is not granted to derived classes.
class TwoDShape {
// these are now private
double width;
double height;
public:
void showDim() {
cout << "Width and height are " <<
width << " and " << height << "\n";
}
};
// Triangle is derived from TwoDShape.
class Triangle : public TwoDShape {
public:
char style[20];
double area() {
return width * height / 2; // Error! Can't access.
}
void showStyle() {
cout << "Triangle is " << style << "\n";
}
};
listing 4
// Access private data through accessor functions.
#include <iostream>
#include <cstring>
using namespace std;
// A class for two-dimensional objects.
class TwoDShape {
// these are private
double width;
double height;
public:
void showDim() {
cout << "Width and height are " <<
width << " and " << height << "\n";
}
// accessor functions
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
};
// Triangle is derived from TwoDShape.
class Triangle : public TwoDShape {
public:
char style[20];
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
cout << "Triangle is " << style << "\n";
}
};
int main() {
Triangle t1;
Triangle t2;
t1.setWidth(4.0);
t1.setHeight(4.0);
strcpy(t1.style, "isosceles");
t2.setWidth(8.0);
t2.setHeight(12.0);
strcpy(t2.style, "right");
cout << "Info for t1:\n";
t1.showStyle();
t1.showDim();
cout << "Area is " << t1.area() << "\n";
cout << "\n";
cout << "Info for t2:\n";
t2.showStyle();
t2.showDim();
cout << "Area is " << t2.area() << "\n";
return 0;
}
listing 5
// Demonstrate public inheritance.
#include <iostream>
using namespace std;
class B {
int i, j;
public:
void set(int a, int b) { i = a; j = b; }
void show() { cout << i << " " << j << "\n"; }
};
class D : public B {
int k;
public:
D(int x) { k = x; }
void showk() { cout << k << "\n"; }
// i = 10; // Error! i is private to B and access is not allowed.
};
int main()
{
D ob(3);
ob.set(1, 2); // access member of base class
ob.show(); // access member of base class
ob.showk(); // uses member of derived class
return 0;
}
listing 6
// Use private inheritance. This program won't compile.
#include <iostream>
using namespace std;
class B {
int i, j;
public:
void set(int a, int b) { i = a; j = b; }
void show() { cout << i << " " << j << "\n"; }
};
// Public elements of B become private in D.
class D : private B {
int k;
public:
D(int x) { k = x; }
void showk() { cout << k << "\n"; }
};
int main()
{
D ob(3);
ob.set(1, 2); // Error, can't access set()
ob.show(); // Error, can't access show()
return 0;
}
listing 7
// Demonstrate protected members.
#include <iostream>
using namespace std;
class B {
protected:
int i, j; // private to B, but accessible to D
public:
void set(int a, int b) { i = a; j = b; }
void show() { cout << i << " " << j << "\n"; }
};
class D : public B {
int k;
public:
// D may access B's i and j
void setk() { k = i*j; }
void showk() { cout << k << "\n"; }
};
int main()
{
D ob;
ob.set(2, 3); // OK, set() is public in B
ob.show(); // OK, show is public B
ob.setk();
ob.showk();
return 0;
}
listing 8
// Add a constructor to Triangle.
#include <iostream>
#include <cstring>
using namespace std;
// A class for two-dimensional objects.
class TwoDShape {
// these are private
double width;
double height;
public:
void showDim() {
cout << "Width and height are " <<
width << " and " << height << "\n";
}
// accessor functons
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
};
// Triangle is derived from TwoDShape.
class Triangle : public TwoDShape {
char style[20]; // now private
public:
// Constructor for Triangle.
Triangle(char *str, double w, double h) {
// Initialize the base class portion.
setWidth(w);
setHeight(h);
// Initialize the derived class portion.
strcpy(style, str);
}
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
cout << "Triangle is " << style << "\n";
}
};
int main() {
Triangle t1("isosceles", 4.0, 4.0);
Triangle t2("right", 8.0, 12.0);
cout << "Info for t1:\n";
t1.showStyle();
t1.showDim();
cout << "Area is " << t1.area() << "\n";
cout << "\n";
cout << "Info for t2:\n";
t2.showStyle();
t2.showDim();
cout << "Area is " << t2.area() << "\n";
return 0;
}
listing 9
// Add a constructor to TwoDShape.
#include <iostream>
#include <cstring>
using namespace std;
// A class for two-dimensional objects.
class TwoDShape {
// these are private
double width;
double height;
public:
// Constructor for TwoDShape.
TwoDShape(double w, double h) {
width = w;
height = h;
}
void showDim() {
cout << "Width and height are " <<
width << " and " << height << "\n";
}
// accessor functions
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
};
// Triangle is derived from TwoDShape.
class Triangle : public TwoDShape {
char style[20]; // now private
public:
// Constructor for Triangle.
Triangle(char *str, double w,
double h) : TwoDShape(w, h) {
strcpy(style, str);
}
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
cout << "Triangle is " << style << "\n";
}
};
int main() {
Triangle t1("isosceles", 4.0, 4.0);
Triangle t2("right", 8.0, 12.0);
cout << "Info for t1:\n";
t1.showStyle();
t1.showDim();
cout << "Area is " << t1.area() << "\n";
cout << "\n";
cout << "Info for t2:\n";
t2.showStyle();
t2.showDim();
cout << "Area is " << t2.area() << "\n";
return 0;
}
listing 10
// Add a constructor to TwoDShape.
#include <iostream>
#include <cstring>
using namespace std;
// A class for two-dimensional objects.
class TwoDShape {
// these are private
double width;
double height;
public:
// Default constructor.
TwoDShape() {
width = height = 0.0;
}
// Constructor for TwoDShape.
TwoDShape(double w, double h) {
width = w;
height = h;
}
// Construct object with equal width and height.
TwoDShape(double x) {
width = height = x;
}
void showDim() {
cout << "Width and height are " <<
width << " and " << height << "\n";
}
// accessor functions
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
};
// Triangle is derived from TwoDShape.
class Triangle : public TwoDShape {
char style[20]; // now private
public:
/* A default constructor. This automatically invokes
the default constructor of TwoDShape. */
Triangle() {
strcpy(style, "unknown");
}
// Constructor with three parameters.
Triangle(char *str, double w,
double h) : TwoDShape(w, h) {
strcpy(style, str);
}
// Construct an isosceles triangle.
Triangle(double x) : TwoDShape(x) {
strcpy(style, "isosceles");
}
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
cout << "Triangle is " << style << "\n";
}
};
int main() {
Triangle t1;
Triangle t2("right", 8.0, 12.0);
Triangle t3(4.0);
t1 = t2;
cout << "Info for t1: \n";
t1.showStyle();
t1.showDim();
cout << "Area is " << t1.area() << "\n";
cout << "\n";
cout << "Info for t2: \n";
t2.showStyle();
t2.showDim();
cout << "Area is " << t2.area() << "\n";
cout << "\n";
cout << "Info for t3: \n";
t3.showStyle();
t3.showDim();
cout << "Area is " << t3.area() << "\n";
cout << "\n";
return 0;
}
listing 11
// Create a subclass of Vehicle called Truck.
#include <iostream>
using namespace std;
// Declare the Vehicle class.
class Vehicle {
// These are private.
int passengers; // number of passengers
int fuelcap; // fuel capacity in gallons
int mpg; // fuel consumption in miles per gallon
public:
// This is a constructor for Vehicle.
Vehicle(int p, int f, int m) {
passengers = p;
fuelcap = f;
mpg = m;
}
// Compute and return the range.
int range() { return mpg * fuelcap; }
// Accessor functions.
int get_passengers() { return passengers; }
int get_fuelcap() { return fuelcap; }
int get_mpg() { return mpg; }
};
// Use Vehicle to create a Truck specialization.
class Truck : public Vehicle {
int cargocap; // cargo capacity in pounds
public:
// This is a constructor for Truck.
Truck(int p, int f,
int m, int c) : Vehicle(p, f, m)
{
cargocap = c;
}
// Accessor function for cargocap.
int get_cargocap() { return cargocap; }
};
int main() {
// construct some trucks
Truck semi(2, 200, 7, 44000);
Truck pickup(3, 28, 15, 2000);
int dist = 252;
cout << "Semi can carry " << semi.get_cargocap() <<
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -