观察以下代码
以类Rectangle的设计和实现为例:
#include<iostream>
using namespace std;
class Rectangle
{
public: //关键字public 确定了类成员的访问属性;在类对象作用域内,公共成员在类的外部是可访问的;
void RectArea(int& length, int& width)
{
cout << "长方形面积为:" << width * length<< endl;
}
void RectPerimeter(int& length, int& width)
{
cout << "长方形周长为:" << 2 * (width + length) << endl;
};
private:
int width; //声明width, 类型为int;
int length; //声明length, 类型为int;
};
int main()
{
int a, b;
cout << "依次输入长方形的长和宽" << endl;
cin >> a >> b;
Rectangle S;
Rectangle C;
S.RectArea(a, b);
C.RectPerimeter(a, b);
return 0;
}
声明一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象,声明并实现这个类。 在main()函数中声明一个Computer类的对象,调用其成员函数。
#include<iostream>
using namespace std;
class CPU
{
public:
CPU() {cout << "调用CPU构造函数" << endl;}
~CPU() { cout << "调用CPU析构函数" << endl; }
void run() { cout << "CPU开始运行" << endl; }
void stop() { cout << "CPU停止运行" << endl; }
};
class RAM
{
public:
RAM() { cout << "调用RAM构造函数" << endl; }
~RAM() { cout << "RAM析构函数" << endl; }
void run() { cout << "RAM开始运行" << endl; }
void stop() { cout << "RAM停止运行" << endl; }
};
class CDROM
{
public:
CDROM() { cout << "调用CDROM构造函数" << endl; }
~CDROM() { cout << "调用CDROM析构函数" << endl; }
void run() { cout << "CDROM开始运行" << endl; }
void stop() { cout << "CDROM停止运行" << endl; }
};
class Computer
{
public:
Computer() {
cout << "Computer构造函数被调用" << endl;
}
~Computer() {
cout << "Computer析构函数被调用" << endl;
}
void run() {
cout << "Computer开始运行" << endl;
cpu.run();
ram.run();
cdrom.run();
}
void stop() {
cpu.stop();
ram.stop();
cdrom.stop();
cout << "Computer停止运行" << endl;
}
private:
CPU cpu;
RAM ram;
CDROM cdrom;
};
int main()
{
Computer t;
t.run();
t.stop();
return 0;
}
用函数实现判断一个数是否质数,并用 穷举法找出1至100间的质数
#include<iostream>
using namespace std;
void IsPrime(int x = 0)
{
int i;
if (x == 1 || x < 1)//1和0不是质数
{
return ;
}
for (i = 2; i < x; i++)
{
if (x % i == 0)
{
break;
}
}
if (x == i)
{
cout << x << " ";
}
}
int main()
{
int x;
for (x = 0; x <= 100; x++) {
IsPrime(x);
}
}
质数:一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数
我们首先定义一个判断是否质数的函数:IsPrime(x),其中如果x为1或0时直接判断为不是质数
然后定义循环:从2开始,用x对其进行除法运算,如果整除,则不是质数,接着将俩数比较,若相
等,说明除到了它本身才整除,该数是质数
break;//退出循环,接着执行循环结构下面的第一条语句
调试swap函数比较三种参数 传递机制之间的异同
#include<iostream>
using namespace std;
void swap(int& a, int& b)//引用传递
{
int temp;
temp = a;
a = b;
b = temp;
}
void swap(int* a, int* b)//地址传递
{
int temp= *a;
*a = *b;
*b = temp;
}
void swap(int a, int b)//这里我们注意到输入与输出一样,对a,b的值传递是对基本型变量而言的, //传递的是该变量的一个副本,改变副本不影响原变量
{
int temp;
temp = b;
b = a;
a = temp;
}
int main()
{
int a;
int b;
cin >> a >> b;
swap(a, b);
cout << a << " " << b;
return 0;
}
引用传递的是它的别名,指针传递的是它的地址,值传递的只是系统变量中的副本,并不会改变变量的值
上机调试课件中的程序,进一步熟悉函 数的相关概念和VS或devC++编程环境 下C++程序的调试技巧
#include<iostream>
using namespace std;
class rectangle
{
public:
void area(int& length, int& width)
{
cout << "长方形面积为:" << width * length<< endl;
}
void perimeter(int& length, int& width)
{
cout << "长方形周长为:" << 2 * (width + length) << endl;
};
private:
int width;
int length;
};
int main()
{
int a, b;
cout << "依次输入长方形的长和宽" << endl;
cin >> a >> b;
rectangle S;
rectangle C;
S.area(a, b);
C.perimeter(a, b);
return 0;
}