目录
Constructor & Destructor
问题描述
构造函数与析构函数
代码实现
#include <iostream>
using namespace std;
class coordinate
{
public :
coordinate (int x1,int y1) //coordinate:坐标
{
x=x1;
y=y1;
}
coordinate(coordinate &p);
~coordinate()
{
cout<<"Destructor is called\n";
}
int getx()
{
return x;
}
int gety()
{
return y;
}
private:
int x,y;
};
coordinate::coordinate(coordinate &p)
{
x=p.x;
y=p.y;
cout<<"copy-initialization constructor is called\n";
}
int main()
{
coordinate p1(3,4);
coordinate p2(p1);
coordinate p3=p2;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";
return 0;
}
输出和最小的子数列
问题描述
输入一个数列,输出和最小的子数列
程序代码
//输入一组数列,求出和最小的子数列,并输出最小和
#include<iostream>
using namespace std;
const int n = 10;
void swap(int &c, int &d)
{
int temp;
temp = c;
c = d;
d = temp;
}
void bubble_sort(int b[], int n)
{
for (int i = n - 1; i >= 1; --i)
{
for (int j = 0; j < i; ++j)
{
if (b[j] > b[j+1])
swap(b[j], b[j+1]);
}
}
}
int main()
{
cout << "请输入数列" << endl;
int i, k,a[10],ans=0,key=11;
for (i = 0; i < 10; i++)
cin >> a[i];
bubble_sort(&a[0], 10);
cout << "排序后的数列为" << endl;
for (i = 0; i < 10; ++i)
{
cout << a[i]<<" ";
}
for (k = 0; k < 10; ++k)
{
if (a[k] < 0)
{
key = k;
ans += a[k];
}
}
if (key == 11)
cout << "最小子数列和为" << a[0] << endl;
else
cout << "最小子数列和为" << ans << endl;
return 0;
}
Q
如果数列大小不确定
求和:fabonaci数列前20
如题
代码
//sum.cpp
#include<iostream>
using namespace std;
int main()
{
int n,m;
int *pi = new int[20];
pi[0] = 1;
pi[1] = 1;
cout << pi[0] << pi[1] << endl;
for (n=2 ; n < 20 ; n++)
{
pi[n] = pi[n - 1] + pi[n - 2];
cout << pi[n] << endl;
}
delete pi;
cin >> m;
return 0;
}
MyArray
动态创建数组
代码
#include<iostream>
#include<string>
using namespace std;
class MyArray {
public:
MyArray(int leng);
~MyArray();
void Input();
void Display(string);
protected:
int *alist;
int length;
};
MyArray::MyArray(int leng)
{
if (leng <= 0)
{
cout << "error length";
exit(1);
}
alist = new int[leng];
length = leng;
if (alist == NULL)
{
cout << "assign failure";
exit(1);
}
cout << "MyArray 类对象已创建。" << endl;
}
MyArray::~MyArray()
{
delete[] alist;
cout << "MyArray 类对象被撤销。" << endl;
}
void MyArray::Display(string str)
{
int i;
int *p = alist;
cout << str << length << " numbers are :";
for (i = 0; i<length; i++, p++)
cout << *p << " ";
cout << endl;
}
void MyArray::Input()
{
cout << "Please input " << length << " numbers :" << endl;
int i;
int *p = alist;
for (i = 0; i<length; i++, p++)
cin >> *p;
}
class SortArray :virtual public MyArray {
public:
void Sort();
SortArray(int leng) :MyArray(leng)
{
len = leng;
cout << "SortArray类对象已创建。" << endl;
}
virtual~SortArray();
private:
int len;
int *sp;
};
SortArray::~SortArray()
{
cout << "SortArray类对象被撤销。" << endl;
}
void SortArray::Sort()
{
int q;
sp = alist;
for (int i = 0; i<len; i++)
{
for (int j = 0; j<len - 1; j++)
{
if (*(sp + j)>*(sp + j + 1))
{
q = *(sp + j);
*(sp + j) = *(sp + j + 1);
*(sp + j + 1) = q;
}
}
}
}
class ReArray :virtual public MyArray {
public:
void Reverse();
ReArray(int leng) :MyArray(leng)
{
len = leng;
void Reverse();
cout << "ReArray类对象已创建。" << endl;
}
virtual ~ReArray();
private:
int len;
int *bp;
};
ReArray::~ReArray()
{
cout << "ReArray类对象被撤销。" << endl;
}
void ReArray::Reverse()
{
int q;
bp = alist;
for (int i = 0; i<len / 2; i++)
{
q = *(bp + i);
*(bp + i) = *(bp + len - i - 1);
*(bp + len - i - 1) = q;
}
}
class Average :virtual public MyArray {
public:
void Aver();
Average(int leng) :MyArray(leng)
{
len = leng;
cout << "Average类对象已创建。" << endl;
}
virtual ~Average();
private:
int len;
int *cp;
};
Average::~Average()
{
cout << "Average类对象被撤销。" << endl;
}
void Average::Aver()
{
double q = 0;
cp = alist;
for (int i = 0; i<len; i++)
{
q = q + *cp;
cp++;
}
q = q / len;
cout << "The average of the " << q << endl;
}
class NewArray :public ReArray, public SortArray, public Average
{
public:
NewArray(int leng);
~NewArray();
};
NewArray::NewArray(int leng) :MyArray(leng), SortArray(leng), ReArray(leng), Average(leng)
{
cout << "\n新数组正在创建。\n";
}
NewArray::~NewArray()
{
cout << "\n新数组已被清空。\n";
}
int main()
{
int leng;
cout << "How many numbers do you want to enter :";
cin >> leng;
NewArray a(leng);
a.Input();
a.Display("These ");
a.Sort();
a.Display("The sorted ");
a.Reverse();
a.Display("After inversion ,the ");
a.Aver();
return 0;
}
noname1
代码
#include<iostream>
using namespace std;
class Base {
public:
void setx(int i)
{
x = i;
}
int getx()
{
return x;
}
public:
int x;
};
class Derived :public Base {
public:
void sety(int i)
{
y = i;
}
int gety()
{
return y;
}
void show()
{
cout << "Base::x=" << x << endl;
} //语句1
public:
int y;
};
int main()
{
Derived bb; //语句2
bb.setx(16); //语句3
bb.sety(25); //语句4
bb.show(); //语句5
cout << "Base::x=" << bb.x << endl; //语句6
cout << "Derived::y=" << bb.y << endl; //语句7
cout << "Base::x=" << bb.getx() << endl; //语句8
cout << "Derived::y=" << bb.gety() << endl; //语句9
return 0;
}
noname2
代码
#include<iostream>
#include <string> //必须加这个头文件
using namespace std;
class Person{
public:
void input()
{
cout << "Num:";
cin >> number;
cout << "Name:";
cin >> name;
cout << "Sex:";
cin >> sex;
cout << "Age:";
cin >> age;
}
void print()
{
cout << "Num:" << number << endl;
cout << "Name:" << name << endl;
cout << "Sex:" << sex << endl;
cout << "Age:" << age << endl;
}
protected:
int number,age;
string name,sex;
};
class Student : public Person{
public:
void input1()
{
cout << "Please enter the student's information:" << endl;
Person::input();
cout << "Institute:";
cin >> department;
cout << "Score:";
cin >> score;
}
void print1()
{
cout << "The student's:" << endl;
Person::print();
cout << "Institute:" << department << endl;
cout << "Score:" << score << endl;
}
private:
string department;
float score;
};
class Teacher : public Person{
public:
void input2()
{
cout << "Please enter the teacher's information:" << endl;
Person::input();
cout << "Title:";
cin >> title;
cout << "Department:";
cin >> section;
}
void print2()
{
cout <<"The teacher's:" << endl;
Person::print();
cout << "Title:" << title << endl;
cout << "Department:" << section << endl;
}
private:
string title,section;
};
int main()
{
Student a;
Teacher b;
a.input1();
b.input2();
a.print1();
b.print2();
}