西电软院《面向对象程序设计》课程复习(1)
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
4
1 函数的基本问题
3. (1) Implement a function: int atoi (const char* str); whiຫໍສະໝຸດ Baiduh converts a string str (in decimal format) to its corresponding integer and return the integer. (2) Write a test program which take an integral string from command-line argument, and print the calling result of atoi().
7
2 Namespace基本问题
namespace a { void f(); int g(double x); }
namespace b { int f(); double y; }
using namespace a; using a::f();
8
3 类的基本问题
1. Implement a class Stack , where a stack is a Last In, First Out list of zero or more elements. The Stack class should provide following methods: (1) void push( const int& t); // insert an element to a stack (2) int pop(); // delete an element from a stack and return it (3) bool empty(); // check a stack is empty or not (4) bool full(); // check a stack is full or not (5) int getNumbers(); // return the current number of elements in a stack
14
3 类的基本问题
class Sub1 : public Base { char* cpString; public: void printOn() { printf("<%d: %s>\n", iBody, cpString); } Sub1(int i, char* s) : Base(i) { cpString = new char[strlen(s)+1]; strcpy(cpString, s); } }; class Sub2 : public Base { short sShort; public: void printOn(){ printf("<%d and %d>\n", sShort, iBody); } Sub2(int i, short s) : Base(i),sShort(s) {} };
2
1 函数的基本问题
void findChar(const char cpString[], const char cChar) { int i; // 作用域概念 for (i = 0; cpString*i+ != ‘\0‘; i++) if (cpString[i] == cChar) { // 常见的操作符使用熟练程度 // … } void find2Char(const char cpString[], const char cChar) { int i = 0; for (const char* p = cpString; *p != ‘\0‘; p++, i++) // 类型匹配概念;dereferencing;指针操作 if (*p == cChar) { // dereferencing printf("The index is %d.\n", i); return; } printf("In the string \"%s\" having %d char‘s, no ‘%c‘ has been found.\n" , cpString, i, cChar); }
9
3 类的基本问题
class Stack{ public: enum { MaxStack = 5 }; void Stack() { top = -1; } void push(const int& t){ if ( full() ) { return ; } arr[++top] = t; } int pop( ){ if ( empty() ) { return dummy_val; } return arr[top--]; } bool empty() { return top < 0 ;} bool full() { return top >= MaxStack - 1; } int getNumbers() { return top + 1; } private: int top, int arr[ MaxStack ]; int dummy_val; };
void findChar(const char cpString[], const char cChar) { for (int i = 0; cpString*i+ != ‘\0‘; i++) if (cpString[i] = cChar) { printf("The index is %d.\n", i); return; } printf("In the string \"%s\" having %d char‘s, no ‘%c‘ has been found.\n" , cpString, i, cChar); }
10
3 类的基本问题
2. According to the interface of a Date class, implement the Date class. class Date { //… public: Date(int y=1970, int m=1, int d=1); //构造器 boolean isLeapYear(const Date& d); //判断对象d的年属性是否是闰年 void print(); //打印日期对象的数据成员值 };
5
1 函数的基本问题
#include <cstdio> #include <iostream> using namespace std; int atoi(const char* str){ int result = 0, len = strlen(str); int sign =1; if( str[0] == '-' ) { str++; sign = -1; len--; } else if( str*0+ == ‘+’) , str++; sign = 1; len--;} for(int i=0; i<len; i++){ result = result * 10 + str[i] - '0' ; } return sign * result; }
15
3 类的基本问题
In a company, there are two types of people: employee and manager. Notice that a manager is also an employee. Every employee has following basic information: name,age,workYear( 工作年限) and depNo(部门号).Moreover, a manager has other some attributes: level(级别) and a array of employees managed. A object of class Employee or Manager has following behaviors: //…. Please base on the above objects behaviors to define and implement class Employee and Manager respectively.
13
3 类的基本问题
试完成类Sub1和Sub2的定义和操作的实现代码,使之能符合 下面程序及在注释中描述的运行结果的要求: main() { Sub1 s1(1000, "This is an object of Sub1"); Sub2 s2(2000, 10); s1.printOn(); // 此时显示出: <1000: This is an object of Sub1> s2.printOn(); // 此时显示出: <10 and 2000> }
3
1 函数的基本问题
2. 阅读下面的程序,写出main的输出。 int h(int iCount, const int* ipIndex, int& irObj) { iCount %= 2; if (*ipIndex >= 5) irObj += iCount; return *ipIndex + irObj; } main() { int i, j = 1, k = 2, m; for (i = 0; i < 10; i++, j++) { m = h(i, &j, k); printf("i = %2d, j = %2d, k = %2d, m = %2d.\n", i, j, k, m); } }
11
3 类的基本问题
class Date{ int year, month, day; public: Date(int y=1970, int m=1, int d=1) { year=y, month=m, day=d; } bool isLeapYear(const Date& d) { if(d.year%400 == 0) return true; if(d.year%4==0 && d.year%100!=0) return true; return false; } void print() { cout << year << '/' << month << '/' << day << endl; } };
12
3 类的基本问题
已知类的定义如下: class Base { protected: int iBody; public: virtual void printOn() = 0; Base(int i = 0) : iBody(i) {} }; class Sub1 : public Base { // … public: void printOn(); Sub1(int i, char* s); }; class Sub2 : public Base { // … public: void printOn(); Sub2(int i, short s); };
6
1 函数的基本问题
int main(int argc, char *argv[]) { if( argc <= 1 ) { cout << "Must has 1 arguments!" << endl; return 1; } printf("atoi(\"%s\")=%d\n", argv[1], atoi(argv[1])); return 0; }
《C++面向对象程序设计》 课程复习
西安电子科技大学软件工程研究所
李雁妮
Email: yannili@mail.xidian.edu.cn
1 函数的基本问题
1. 对下面的函数findChar,改正你所发现的错误,并写出另一个 函数find2Char,使之改用指针方式来访问cpString的元素,且 功能与改正后的函数findChar等价。
1 函数的基本问题
3. (1) Implement a function: int atoi (const char* str); whiຫໍສະໝຸດ Baiduh converts a string str (in decimal format) to its corresponding integer and return the integer. (2) Write a test program which take an integral string from command-line argument, and print the calling result of atoi().
7
2 Namespace基本问题
namespace a { void f(); int g(double x); }
namespace b { int f(); double y; }
using namespace a; using a::f();
8
3 类的基本问题
1. Implement a class Stack , where a stack is a Last In, First Out list of zero or more elements. The Stack class should provide following methods: (1) void push( const int& t); // insert an element to a stack (2) int pop(); // delete an element from a stack and return it (3) bool empty(); // check a stack is empty or not (4) bool full(); // check a stack is full or not (5) int getNumbers(); // return the current number of elements in a stack
14
3 类的基本问题
class Sub1 : public Base { char* cpString; public: void printOn() { printf("<%d: %s>\n", iBody, cpString); } Sub1(int i, char* s) : Base(i) { cpString = new char[strlen(s)+1]; strcpy(cpString, s); } }; class Sub2 : public Base { short sShort; public: void printOn(){ printf("<%d and %d>\n", sShort, iBody); } Sub2(int i, short s) : Base(i),sShort(s) {} };
2
1 函数的基本问题
void findChar(const char cpString[], const char cChar) { int i; // 作用域概念 for (i = 0; cpString*i+ != ‘\0‘; i++) if (cpString[i] == cChar) { // 常见的操作符使用熟练程度 // … } void find2Char(const char cpString[], const char cChar) { int i = 0; for (const char* p = cpString; *p != ‘\0‘; p++, i++) // 类型匹配概念;dereferencing;指针操作 if (*p == cChar) { // dereferencing printf("The index is %d.\n", i); return; } printf("In the string \"%s\" having %d char‘s, no ‘%c‘ has been found.\n" , cpString, i, cChar); }
9
3 类的基本问题
class Stack{ public: enum { MaxStack = 5 }; void Stack() { top = -1; } void push(const int& t){ if ( full() ) { return ; } arr[++top] = t; } int pop( ){ if ( empty() ) { return dummy_val; } return arr[top--]; } bool empty() { return top < 0 ;} bool full() { return top >= MaxStack - 1; } int getNumbers() { return top + 1; } private: int top, int arr[ MaxStack ]; int dummy_val; };
void findChar(const char cpString[], const char cChar) { for (int i = 0; cpString*i+ != ‘\0‘; i++) if (cpString[i] = cChar) { printf("The index is %d.\n", i); return; } printf("In the string \"%s\" having %d char‘s, no ‘%c‘ has been found.\n" , cpString, i, cChar); }
10
3 类的基本问题
2. According to the interface of a Date class, implement the Date class. class Date { //… public: Date(int y=1970, int m=1, int d=1); //构造器 boolean isLeapYear(const Date& d); //判断对象d的年属性是否是闰年 void print(); //打印日期对象的数据成员值 };
5
1 函数的基本问题
#include <cstdio> #include <iostream> using namespace std; int atoi(const char* str){ int result = 0, len = strlen(str); int sign =1; if( str[0] == '-' ) { str++; sign = -1; len--; } else if( str*0+ == ‘+’) , str++; sign = 1; len--;} for(int i=0; i<len; i++){ result = result * 10 + str[i] - '0' ; } return sign * result; }
15
3 类的基本问题
In a company, there are two types of people: employee and manager. Notice that a manager is also an employee. Every employee has following basic information: name,age,workYear( 工作年限) and depNo(部门号).Moreover, a manager has other some attributes: level(级别) and a array of employees managed. A object of class Employee or Manager has following behaviors: //…. Please base on the above objects behaviors to define and implement class Employee and Manager respectively.
13
3 类的基本问题
试完成类Sub1和Sub2的定义和操作的实现代码,使之能符合 下面程序及在注释中描述的运行结果的要求: main() { Sub1 s1(1000, "This is an object of Sub1"); Sub2 s2(2000, 10); s1.printOn(); // 此时显示出: <1000: This is an object of Sub1> s2.printOn(); // 此时显示出: <10 and 2000> }
3
1 函数的基本问题
2. 阅读下面的程序,写出main的输出。 int h(int iCount, const int* ipIndex, int& irObj) { iCount %= 2; if (*ipIndex >= 5) irObj += iCount; return *ipIndex + irObj; } main() { int i, j = 1, k = 2, m; for (i = 0; i < 10; i++, j++) { m = h(i, &j, k); printf("i = %2d, j = %2d, k = %2d, m = %2d.\n", i, j, k, m); } }
11
3 类的基本问题
class Date{ int year, month, day; public: Date(int y=1970, int m=1, int d=1) { year=y, month=m, day=d; } bool isLeapYear(const Date& d) { if(d.year%400 == 0) return true; if(d.year%4==0 && d.year%100!=0) return true; return false; } void print() { cout << year << '/' << month << '/' << day << endl; } };
12
3 类的基本问题
已知类的定义如下: class Base { protected: int iBody; public: virtual void printOn() = 0; Base(int i = 0) : iBody(i) {} }; class Sub1 : public Base { // … public: void printOn(); Sub1(int i, char* s); }; class Sub2 : public Base { // … public: void printOn(); Sub2(int i, short s); };
6
1 函数的基本问题
int main(int argc, char *argv[]) { if( argc <= 1 ) { cout << "Must has 1 arguments!" << endl; return 1; } printf("atoi(\"%s\")=%d\n", argv[1], atoi(argv[1])); return 0; }
《C++面向对象程序设计》 课程复习
西安电子科技大学软件工程研究所
李雁妮
Email: yannili@mail.xidian.edu.cn
1 函数的基本问题
1. 对下面的函数findChar,改正你所发现的错误,并写出另一个 函数find2Char,使之改用指针方式来访问cpString的元素,且 功能与改正后的函数findChar等价。