我修改的程序如下:
#include<iostream> using namespace std; class Date { public: Date(int = 1, int = 1, int = 2005); void display(); private: int month; int day; int year; }; Date::Date(int m, int d, int y):month(m), day(d), year(y) { } void Date::display() { cout << month << "/" << day << "/" << year << endl; } int main() { Date d1(10, 13, 2005); Date d2(12, 30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; }方法一(一般法):
#include<iostream> using namespace std; class Student{ public: Student(int s, int g):sno(s), grade(g){} // void output const(); // private: int sno; float grade; }; //void Student::output const() //{ // cout << "No." << sno << "'s grade is :" << grade << endl; //} void output(const Student *p) { cout << "No." << p->sno << "'s grade is :" << p->grade << endl; } int main() { Student s[5] = { Student(1, 85), Student(2, 90), Student(3, 95), Student(4, 99), Student(5, 100) }; const Student * p; p = &s[0]; output(p); p += 2; output(p); p += 2; output(p); // p = &s[4]; // output(p); return 0; }方法二(使用了const保证类的成员数据的安全性):
#include<iostream> using namespace std; class Student{ public: Student(int s, int g):sno(s), grade(g){} void output()const; private: int sno; float grade; }; void Student::output()const { cout << "No." << sno << "'s grade is :" << grade << endl; } int main() { Student s[5] = { Student(1, 85), Student(2, 90), Student(3, 95), Student(4, 99), Student(5, 100) }; const Student * p; p = &s[0]; p->output(); p += 2; p->output(); p += 2; p->output(); return 0; }运行结果如下:
运行结果如下:
运行结果如下:
(1)常对象 (2)我修改后的程序如下:
#include<iostream> using namespace std; class Student{ public: Student(int n, float s):num(n), score(s){} void change(int n, float s) const { num = n; score = s; } void display() const { cout << num << " " << score << endl; } private: mutable int num; mutable float score; }; int main() { const Student stud(101, 78.5); stud.display(); stud.change(101, 80.5); stud.display(); return 0; }运行结果美丽: (3)运行结果如下: (4)我修改的C++代码如下:
#include<iostream> using namespace std; class Student{ public: Student(int n, float s):num(n), score(s){} void change(int n, float s) const { num = n; score = s; } void display() const { cout << num << " " << score << endl; } private: mutable int num; mutable float score; }; int main() { const Student stud(101, 78.5); stud.display(); const Student *p = &stud; //Student类常对象指针 (*p).change(101, 80.5); p->display(); return 0; }运行结果与上题一致
(5)
#include<iostream> using namespace std; class Student{ public: Student(int n, float s):num(n), score(s){} void change(int n, float s) const { num = n; score = s; } void display() const { cout << num << " " << score << endl; } private: mutable int num; mutable float score; }; int main() { Student stud(101, 78.5); stud.display(); Student * const p = &stud; //Student类常变量指针 (*p).change(101, 80.5); p->display(); return 0; }运行结果与上题一致
详细C++代码如下:
#include<iostream> using namespace std; class Student{ public: Student(int n, float s):num(n), score(s){} void change(int n, float s) { num = n; score = s; } void display() { cout << num << " " << score << endl; } private: int num; float score; }; void fun(Student &s, int n, float score) { s.display(); s.change(n, score); s.display(); } int main() { Student stud(101, 78.5); fun(stud, 101, 80.5); return 0; }不知道是我没理解正确题目还是题目有问题,那句“对于一次购10以上的客户,还可以享受9.8折优惠”,然后再看所给的销售情况,销售情况里只有各个销货员总的销货件数,并没有涉及到客户是否是购了10件以上的,所以这题我就放飞自我按我自己理解的写了(但不得不说,谭浩强的C系列的书真的好多问题,这里不推荐初学者以这个为教材,我推荐C++ Primer)
详细C++代码如下:
#include<iostream> using namespace std; class Sell{ public: static float aver(); static void display(); void total(); Sell(int n, int q, float p): num(n), quantity(q), price(p){} private: static float discount; //折扣 static float sum; //总销售款sum static int n; //销售总件数n int num; int quantity; float price; }; float Sell::discount = 1.0; float Sell::sum = 0; int Sell::n = 0; float Sell::aver() { return sum / n; } void Sell::display() { cout << "总销售款:" << sum << endl; cout << "每件商品的平均售价:" << aver() << endl; } void Sell::total() { if(quantity > 10) discount = 0.98; else discount = 1.0; sum += price * discount * quantity; n += quantity; } int main() { Sell s[3] = { Sell(101, 5, 23.5), Sell(102, 12, 24.56), Sell(103, 100, 21.5) }; s[0].total(); s[1].total(); s[2].total(); s[2].display(); return 0; }运行结果如下:
运行结果如下:
根据题意:将Time类声明为Date类的友元类,代码就是例3.13(严重怀疑谭*强语文不好)
#include<iostream> using namespace std; class Date; class Time{ public: Time(int, int, int); void display(Date &); private: int hour; int minute; int sec; }; class Date{ public: Date(int, int, int); friend void Time::display(Date &); private: int month; int day; int year; }; Time::Time(int h, int m, int s) { hour = h; minute = m; sec = s; } void Time::display(Date &d) { cout << d.month << "/" << d.day << "/" << d.year << endl; cout << hour << ":" << minute << ":" << sec << endl; } Date::Date(int m, int d, int y) { month = m; day = d; year = y; } int main() { Time t1(10, 13, 56); Date d1(12, 25, 2004); t1.display(d1); return 0; }如果是将Date类声明为Time的友元类,则C++代码如下:
#include<iostream> using namespace std; class Time; class Date{ public: Date(int, int, int); void display(Time &); private: int month; int day; int year; }; class Time{ public: Time(int, int, int); friend void Date::display(Time &); private: int hour; int minute; int sec; }; Time::Time(int h, int m, int s) { hour = h; minute = m; sec = s; } void Date::display(Time &t) { cout << month << "/" << day << "/" << year << endl; cout << t.hour << ":" << t.minute << ":" << t.sec << endl; } Date::Date(int m, int d, int y) { month = m; day = d; year = y; } int main() { Time t1(10, 13, 56); Date d1(12, 25, 2004); d1.display(t1); return 0; }运行结果同上题
改写后的C++代码如下:
#include<iostream> using namespace std; template<class numtype> class Compare{ public: Compare(numtype a, numtype b); numtype max(); numtype min(); private: numtype x, y; }; template<class numtype> Compare<numtype>::Compare(numtype a, numtype b) { x = a; y = b; } template<class numtype> numtype Compare<numtype>::max() { return x > y ? x : y; } template<class numtype> numtype Compare<numtype>::min() { return x < y ? x : y; } int main() { Compare<int> cmp1(3, 7); cout << cmp1.max() << "is the Maximum." << endl; cout << cmp1.min() << "is the Minimun." << endl; Compare<float> cmp2(45.78, 93.6); cout << cmp2.max() << "is the Maximum." << endl; cout << cmp2.min() << "is the Minimun." << endl; Compare<char> cmp3('a', 'A'); cout << cmp3.max() << "is the Maximum." << endl; cout << cmp3.min() << "is the Minimun." << endl; return 0; }