c++
C++是一门强大的面向对象编程语言,由C语言发展而来,”++“意为更进一步发展,C++充分吸收了C语言的优点,同时汲取了面向对象的编程思想,其后拥有STL库的强大支撑,具有封装、继承、多态三大特性,常以高效能著称。
-
下面程序中A是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:B1 calledB2 called#include<iostream.h> class A { public: ______________(1)____________; }; class B1:public A { public: void display() {cout<”B1 called”<<endl}; }; class B2:public A { public: void display() {cout<<”B2 called”<<endl}; }; void show(_________(2)________) { p->display(); } void main() { B1 b1; B2 b2; A* p[2]={&b1,&b2}; for(int i=0;i<2;i++) show(p[i]); }
-
请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:The square’s area is 4#include<iostream.h> class Rectangle { double W,H; public: Rectangle(double w=0,h=0){W=w;H=h;} void show(){cout<<W*H;} }; class Square:public Rectangle { public: Square(double r):__________(1)_________{}; void show() { cout<<”The square’s area is “; _______________(2)___________; } }; void main() { Square s(2); s.show(); }
-
根据下列Vector类定义,编程完成Vector类的具体实现:class Vector { friend ostream &operator<<(ostream &out, const Vector &v); private: int *data; int size; public: Vector(); Vector(int a[],int n); Vector(const Vector &s); ~Vector(); Vector &operator=(const Vector &v); int &operator[](int index); };
-
按下列要求编程,实现类的定义,并在主函数中测试这个类。定义一个描述日期的类Date,包括的数据成员有年(year)、月(month)和日(day),并实现如下功能函数;(1)日期对象初始化;(2)设置日期;(3)以year/month/day形式输出日期;(4)判断闰年。
-
程序阅读题1、#include<iostream.h> class Test { private: int num; public: Test(int n=0){num=n;num++} ~Test(){cout<<″Destructor is active,number=″<<num<<endl;} }; void main() { Test x[2]; cout<<″Exiting main″<<endl; }2、#include <iostream.h> class A { public: virtual void fun (int data){cout<<”class A:”<<data<<endl;} void fun(char *str){ cout<<”class A:”<<str<<endl; } }; class B: public A { public: void fun() {cout<<”class B”<<endl;} void fun(int data) { cout<<”class B:”<<data<<endl;} void fun(char *str){ cout<<”class B:”<<str<<endl;} }; void main() { A *pA; B b; pA=&b; pA->fun(1); pA->fun(“Hello”); }3、#include <iostream.h> void main() { cout.fill('*'); cout.width(10); cout<<"123.45"<<endl; cout.width(8); cout<<"123.45"<<endl; cout.width(4); cout<<"123.45"<<endl; }4、从键盘输入10,给出程序的输出结果。#include<iostream> #include<vector> #include<iterator> using namespace std; main() { vector<int> v; int i,j,n; cin>>n; for(i=1;i<=n;i++) { if(i%2!=0) v.push_back(x); } vector<int>::iterator p; for(p=v.begin();p!=v.end();p++) cout<<*p<<endl; }5、#include<iostream> using namespace std; class Sample { private: int i; static int count; public: Sample(); void display(); }; Sample::Sample() { i=0; count++; } void Sample::display() { cout<<"i="<<i++<<",count="<<count<<endl; } int Sample::count=0; void main() { Sample a,b; a.display(); b.display(); }
-
请在下面的横线处填上适当内容,以使类的定义完整。class Array { Int* ptr; Int size; public: Array(){size=0; ptr=NULL;} Array(int n){size=n;ptr=new int[size];} Array(______(1)_______)//复制初始化构造函数 { size=a.size; ptr=new int[size]; for(int i=0;i<size;i++) ________(2)________;//将源对象的动态数组内容复制到目标对象 } };
-
请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为5。#include<iostream.h> class Integer { int x; public: Integer(int a=0){x=a;} void display(){cout<<x<<endl;} _____________(1)_____________; }; Integer Max(Integer a,Integer b) { if(__________(2)_________) return a; return b; } void main() { Integer a(3),b(5),c; c=Max(a,b); c.display(); }
-
请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:Name:王小明Grade:90#include<iostream.h> #include<string.h> class Person { char name[20] public: Person(char* s){strcpy(name,s);} void display(){cout<<”Name:”<<name<<endl;} }; class Student:public Person { int grade; public: Student(char* s,int g):________(1)_____{grade=g;} void display(){ ___________(2)___________; cout<<”Grade:”<<grade<<endl; } }; void main() { Student s(“王小明”,90); s.display(); }
-
下面程序中A是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:B1 calledB2 called#include<iostream.h> class A { public: ______________(1)____________; }; class B1:public A { public: void display(){cout<”B1 called”<<endl; }; class B2:public A { public: void display(){cout<<”B2 called”<<endl; }; void show(_________(2)________) { p->display(); } void main() { B1 b1; B2 b2; A* p[2]={&b1,&b2}; for(int i=0;i<2;i++) show(p[i]); }
-
请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:2,14,3#include<iostream.h> class A { int a; public: A(int i=0){a=i;} Int Geta(){return a;} }; class B { A a; int b; public: B(int i=0,int j=0):___________(1)____________{} void display(){cout<<a.Geta()<<’,’<<b<<endl;} }; void main() { B b[2]={B(1,2),B(3,4)}; for(int i=0;i<2;i++) _____________(2)______________; }
-
定义一个矩阵类(Matrix),设计相关的构造函数、析构函数等,采用运算符重载方式实现矩阵的加、减运算。编写main函数,对以上所有功能进行测试。注意:按照实际情况大小使用内存空间,矩阵的加、减运算不要造成内存浪费。#include <iostream> #include <vector> #include <cstdlib> using namespace std; class Matrix { public: Matrix (int row, int col); //构造row x col大小矩阵 Matrix operator + (const Matrix &rhs) const;//相同大小矩阵相加 Matrix operator - (const Matrix &rhs) const;//相同大小矩阵相减 void Display () const;//显示矩阵 //矩阵元素,可作为右值 const int &at (int row, int col) const { return _datas [row][col]; } //矩阵元素,可作为左值 int& at (int row, int col) { return _datas [row][col]; } //矩阵行数 int GetRowCount () const { return _datas.size (); } //矩阵列数 int GetColCount () const { return _datas [0].size (); } private: //存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值 vector<vector<int> > _datas; }; Matrix::Matrix (int row, int col) { for (int i = 0; i < row; i++) { vector<int> dataRow (col); _datas.push_back (dataRow); } } Matrix Matrix::operator + (const Matrix &rhs) const { Matrix result (this->GetRowCount(), this->GetColCount()); for (unsigned int i = 0; i < _datas.size (); i++) { for (unsigned int j = 0; j < _datas [i].size (); j++) { result.at(i,j) = this->at(i, j) + rhs.at (i, j); } } return result; } Matrix Matrix::operator - (const Matrix &rhs) const { Matrix result (this->GetRowCount(), this->GetColCount()); for (unsigned int i = 0; i < _datas.size (); i++) { for (unsigned int j = 0; j < _datas [i].size (); j++) { result.at(i,j) = this->at(i, j) - rhs.at (i, j); } } return result; } void Matrix::Display () const { for (unsigned int i = 0; i < _datas.size (); i++) { for (unsigned int j = 0; j < _datas [i].size (); j++) { cout.width (5); cout << at (i,j); } cout << endl; } cout << endl; } int main() { Matrix A (5, 6), B (5, 6); int i, j; for (i = 0; i < A.GetRowCount(); i++) for (j = 0; j < A.GetColCount(); j++) A.at(i, j) = rand () % 100 ; for (i = 0; i < A.GetRowCount(); i++) for (j = 0; j < A.GetColCount(); j++) B.at(i, j ) = rand () % 100; A.Display(); B.Display(); Matrix C = A + B; C.Display(); Matrix D = A - B; D.Display(); return 0; }#include <iostream> #include <vector> #include <cstdlib> using namespace std; class Matrix { public: Matrix (int row, int col); //构造row x col大小矩阵 Matrix operator + (const Matrix &rhs) const;//相同大小矩阵相加 Matrix operator - (const Matrix &rhs) const;//相同大小矩阵相减 void Display () const;//显示矩阵 //矩阵元素,可作为右值 const int &at (int row, int col) const { return _datas [row][col]; } //矩阵元素,可作为左值 int& at (int row, int col) { return _datas [row][col]; } //矩阵行数 int GetRowCount () const { return _datas.size (); } //矩阵列数 int GetColCount () const { return _datas [0].size (); } private: //存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值 vector<vector<int> > _datas; }; Matrix::Matrix (int row, int col) { for (int i = 0; i < row; i++) { vector<int> dataRow (col); _datas.push_back (dataRow); } } Matrix Matrix::operator + (const Matrix &rhs) const { Matrix result (this->GetRowCount(), this->GetColCount()); for (unsigned int i = 0; i < _datas.size (); i++) { for (unsigned int j = 0; j < _datas [i].size (); j++) { result.at(i,j) = this->at(i, j) + rhs.at (i, j); } } return result; } Matrix Matrix::operator - (const Matrix &rhs) const { Matrix result (this->GetRowCount(), this->GetColCount()); for (unsigned int i = 0; i < _datas.size (); i++) { for (unsigned int j = 0; j < _datas [i].size (); j++) { result.at(i,j) = this->at(i, j) - rhs.at (i, j); } } return result; } void Matrix::Display () const { for (unsigned int i = 0; i < _datas.size (); i++) { for (unsigned int j = 0; j < _datas [i].size (); j++) { cout.width (5); cout << at (i,j); } cout << endl; } cout << endl; } int main() { Matrix A (5, 6), B (5, 6); int i, j; for (i = 0; i < A.GetRowCount(); i++) for (j = 0; j < A.GetColCount(); j++) A.at(i, j) = rand () % 100 ; for (i = 0; i < A.GetRowCount(); i++) for (j = 0; j < A.GetColCount(); j++) B.at(i, j ) = rand () % 100; A.Display(); B.Display(); Matrix C = A + B; C.Display(); Matrix D = A - B; D.Display(); return 0; }
-
编程实现小型公司的工资管理。该公司主要有4类人员:经理(manager)、兼职技术人员(technician)、销售员(salesman)和销售经理(salesmanager)。要求存储这些人员的编号、姓名和月工资,计算月工资并显示全部信息。月工资计算办法是:经理拿固定月薪8000元,兼职技术人员按每小时100元领取月薪,销售员按当月销售额的4%提成,销售经理既拿固定月工资也领取销售提成,固定月工资为5000元,销售提成为所管辖部门当月销售总额的千分之五。兼职技术人员一个月工作小时数、销售员一个月销售额、销售经理所管辖部门一个月销售总额由各个类的成员函数完成设置。(要求用抽象类和类继承)#include <iostream> using namespace std; //雇员类 class Employee { protected : int _id; double _salary; string _name; public: Employee(int id,string name) :_id(id),_name(name) {} virtual ~Employee(){} virtual double CalcSalary()=0; }; //经理类 class Manager:public Employee{ public: Manager(int id,string name):Employee(id,name){} virtual double CalcSalary(){ _salary=8000; return _salary; }; }; //兼职技术员类 class Technician:public Employee{ int _workHour; public: Technician(int id,string name):Employee(id,name){} void SetWorkHour(int workHour){ _workHour = workHour; } virtual double CalcSalary(){ _salary=100*_workHour; return _salary; }; }; //销售经理类 class Salesman:public Employee{ double _salesMount; public : Salesman (int id,string name):Employee(id,name){} void SetSalesMount(double salesMount){ _salesMount=salesMount; } virtual double CalcSalary(){ _salary=0.04*_salesMount; return _salary; }; }; class SalesManager:public Employee{ double _salesTotalMount; public: SalesManager (int id,string name):Employee(id,name){} void SetSalesTotalMount(double salesTotalMount){ _salesTotalMount=salesTotalMount; } virtual double CalcSalary(){ _salary =5000 +0.005 * _salesTotalMount; return _salary; }; }; int main() { Manager zhang(1000, "张三"); Technician li(1000, "李四"); Salesman wang(1000, "王五"); SalesManager zhao(1000, "赵六"); li.SetWorkHour(50); wang.SetSalesMount(175000); zhao.SetSalesTotalMount(1500000); Employee *pEmployeeA[]={&zhang,&li,&wang,&zhao}; for (int i = 0; i <4; i++) { cout << pEmployeeA [i] ->CalcSalary() << endl; } }
-
读程序写结果1.#include <iostream.h> class CSample { private: int n; static int k; public: CSample(int i){n=i;k++;} void disp(); }; void CSample::disp() {cout<<"n="<<n<<",k="<<k<<endl;} int CSample::k=0; int main() {CSample a(10),b(20),c(30); a.disp(); b.disp(); c.disp(); }2.#include <iostream.h> class Sample {int n; public: Sample(){} Sample(int m){n=m; } Sample operator--(int) {Sample old=*this; n--; return old; } void disp(){cout<<"n="<<n<<endl;} }; int main() {Sample s(10); s--; s.disp(); }3.#include <iostream.h> #include <string.h> template <class T> T max(T x, T y) { return (x > y ? x : y); } char *max(char *x, char *y) { if (strcmp(x, y) >= 0) return x; else return y; } int main() { cout << max(2, 5) << endl; cout << max(3.14,4.12) << endl; cout << max("China", "Beijing") << endl; }4.#include <stack> #include <iostream> #include <string> using namespace std; int main() { stack<char> s; string str; cin >> str;//从键盘输入字符串LoveChina for(string::iterator iter = str.begin(); iter != str.end(); ++iter) s.push(*iter); while (!s.empty()) { cout << s.top(); s.pop(); } cout << endl; }5.#include <iostream> using namespace std; int divide(int x, int y) { if (y == 0) throw x; return x / y; } int main() {try { cout << "8/3 = " << divide(8, 3) << endl; cout << "6/0 = " << divide(6, 0) << endl; cout << "9/2 = " << divide(9, 2) << endl; } catch (int e) { cout << e << " is divided by zero!" << endl; } cout << "That is ok." << endl; }
-
下面程序用STL的条件计数算法和自定义的函数对象对一个存放在整数向量类对象中的学生成绩进行统计及格人数并显示结果。请在下面程序的横线处填上适当字句,以使程序完整。#include<iostream> #include<vector> #include<algorithm> #include<functional> using namespace std; class Pass { public: ____________________(1)___________________ { return x>=60; } }; int main() { vector<int> a; } void disp(){cout<<"n="<<n<<endl;} }; intmain() {Sample s(10); s--; s.disp(); } int count; a.push_back(78); a.push_back(92); a.push_back(52); count = ____________________(2)___________________; cout<<"count="<<count<<endl; }
-
请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为构造 base 对象调用 base::f()构造 derive 对象调用 derive::f()#include <iostream.h> class base { public: base() {cout<<"构造base对象"<<endl; f(); } _________(1)_________{cout<<"调用base::f()"<<endl;} }; class derive:public base { public: derive() {cout<<"构造derive对象"<<endl; _________(2)_________; } void f(){cout<<"调用derive::f()"<<endl;} }; int main() { derive d; }
-
请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为A Constructor2B Constructor2x1=12x2=2#include <iostream.h> class A {public: A(){cout<<"A Constructor1"<<endl;} A(int i){x1=i;cout<<"A Constructor2"<<endl;}; void dispa(){cout<<"x1="<<x1<<endl;} private: int x1; }; class B:public A { public: B(){cout<<"B Constructor1"<<endl;} B(int i): _____(1)_____{x2=i;cout<<"B Constructor2"<<endl;} void dispb() {_____(2)_____; cout<<"x2="<<x2<<endl; } private: int x2; }; int main() {B b(2); b.dispb(); }
-
请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:n=30#include <iostream.h> template <class T> class Sample {T n; public: Sample(){} Sample(T i){n=i;} Sample<T> operator+(const Sample<T> &); void disp(){cout<<"n="<<n<<endl;} }; template <class T> Sample<T> Sample<T>::operator+(const Sample<T> &s){Sample<T> temp; _______(1)_______; return temp; } int main() {Sample<int> s1(10),s2(20),s3; s3=s1+s2; _______(2)_______; }
-
请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:2/10/2004#include <iostream.h> static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31}; class date {int mo,da,yr; public: date(int m,int d,int y){mo=m;da=d;yr=y;} date(){} void disp(){cout<<mo<<"/"<<da<<"/"<<yr<<endl;} void leap(int a){ if(a%4==0&&a%100!=0||a%400==0) dys[1]=29; else dys[1]=28;} friend date operator+(_______(1)_______) {day+=d.da; d.leap(d.yr); while(_______(2)_______) {d.leap(d.yr); day-=dys[d.mo-1]; if(++d.mo==13) {d.mo=1; d.yr++;} } d.da=day; return d; } }; int main() {date d1(2,10,2003),d2; int n; cin>>n; //从键盘输入365 while(n<0){cin>>n;} d2=d1+n; d2.disp(); }
-
编写程序:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle(长方形)、Trapezoid(梯形)和Triangle(三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。注:主函数中定义如下对象Circle circle(12.6);Square square(3.5);Rectangle rectangle(4.5,8.4);Trapezoid trapezoid(2.0,4.5,3.2);Triangle triangle(4.5,8.4);
-
程序填空:#include <iostream> using namespace std; class A { ______(1)______ char name[80]; public: A( ____(2)______ ) {____(3)______ } }; class B_____(4)_______ { public: B(const char*n)_____(5)_______{} void PrintName( ) {cout<<”name:”<<name<<endl;}; }; void main( ) { B b1(“Ling Li”); b1.PrintName( ) ; }//执行结果:name: Ling Li