目錄
- 目錄
- Constructor & Destructor
-
- 問題描述
- 代碼實作
-
- 輸出和最小的子數列
-
- 問題描述
- 程式代碼
- Q
-
- 求和:fabonaci數列前20
-
- 代碼
-
- MyArray
-
- 代碼
-
- noname1
-
- 代碼
-
- noname2
-
- 代碼
-
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(,);
coordinate p2(p1);
coordinate p3=p2;
cout<<"p3=("<<p3.getx()<<","<<p3.gety()<<")\n";
return ;
}
輸出和最小的子數列
問題描述
輸入一個數列,輸出和最小的子數列
程式代碼
//輸入一組數列,求出和最小的子數列,并輸出最小和
#include<iostream>
using namespace std;
const int n = ;
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 - ; i >= ; --i)
{
for (int j = ; j < i; ++j)
{
if (b[j] > b[j+])
swap(b[j], b[j+]);
}
}
}
int main()
{
cout << "請輸入數列" << endl;
int i, k,a[],ans=,key=;
for (i = ; i < ; i++)
cin >> a[i];
bubble_sort(&a[], );
cout << "排序後的數列為" << endl;
for (i = ; i < ; ++i)
{
cout << a[i]<<" ";
}
for (k = ; k < ; ++k)
{
if (a[k] < )
{
key = k;
ans += a[k];
}
}
if (key == )
cout << "最小子數列和為" << a[] << endl;
else
cout << "最小子數列和為" << ans << endl;
return ;
}
Q
如果數列大小不确定
求和:fabonaci數列前20
如題
代碼
//sum.cpp
#include<iostream>
using namespace std;
int main()
{
int n,m;
int *pi = new int[];
pi[] = ;
pi[] = ;
cout << pi[] << pi[] << endl;
for (n= ; n < ; n++)
{
pi[n] = pi[n - ] + pi[n - ];
cout << pi[n] << endl;
}
delete pi;
cin >> m;
return ;
}
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 <= )
{
cout << "error length";
exit();
}
alist = new int[leng];
length = leng;
if (alist == NULL)
{
cout << "assign failure";
exit();
}
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 = ; i<length; i++, p++)
cout << *p << " ";
cout << endl;
}
void MyArray::Input()
{
cout << "Please input " << length << " numbers :" << endl;
int i;
int *p = alist;
for (i = ; 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 = ; i<len; i++)
{
for (int j = ; j<len - ; j++)
{
if (*(sp + j)>*(sp + j + ))
{
q = *(sp + j);
*(sp + j) = *(sp + j + );
*(sp + j + ) = 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 = ; i<len / ; i++)
{
q = *(bp + i);
*(bp + i) = *(bp + len - i - );
*(bp + len - i - ) = 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 = ;
cp = alist;
for (int i = ; 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 ;
}
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(); //語句3
bb.sety(); //語句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 ;
}
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();
}