#include<iostream>
using namespace std;
void fun() {
static int a = 1;
int i= 5;
i++;
a++;
cout << "i" << i << "a"<<a<<endl;
}
int main()
{
fun();
}
#include <iostream>
using namespace std;
const float PI = 3.1459;
class Circle {
public:
Circle(float r);
float getArea();
private:
float radius;
};
Circle::Circle(float r) {
radius = r;
}
float Circle::getArea() {
float area = PI*radius*radius;
return area;
}
int main() {
Circle cir(10);
cout << cir.getArea() << endl;
return 0;
}
#include<iostream>
using namespace std;
class R {
public:
R(int r1, int r2) :r1(r1), r2(r2) {}
void print();
void print() const;//声明常函数
private:
int r1, r2;
};
void R::print() {
cout << "r1=" << r1 << "r2=" << r2 << endl;
}
void R::print() const {
cout << "const r1=" << r1 << "r2=" << r2 << endl;
}
int main() {
R a(5, 4);
a.print();
R const b(20, 52);//常对象
b.print();
return 0;
}
#include <iostream>
using namespace std;
void RowSum(int a[][4],int nrow){
for (int i = 0; i < nrow; i++) {
int sum = 0;
for (int j = 0; j < 4; j++)
{
sum = 0;
sum += a[i][j];
}
cout << "sum" << i << sum << endl;
a[i][0] = sum;
}
}
int main() {
int table[3][4] = { {1,2,3,4},{2,3,4,5},{3,4,5,6} };
for (int i = 0; i < 3; i++)
{
for (int j= 0; j < 4; j++)
{
}
a[i] = i * 2 - 1;
b[10 - i - 1] = a[i];
}
for (int i = 0; i < 10; i++)
{
cout << "a[]"<<a[i] <<"b[]"<< b[i] << endl;
}
}
#include<iostream>
#include"标头.h"
#include<cmath>
using namespace std;
float lineFit( Point points[], int nPoint) {
float avgX = 0, avgY = 0;
float lxx = 0, lyy = 0, lxy = 0;
for (int i = 0; i < nPoint; i++)
{
avgX += points[i].getX() / nPoint;
avgY += points[i].getY() / nPoint;
}
for (int i = 0; i < nPoint; i++) {
lxx += (points[i].getX() - avgX)*(points[i].getX() - avgX);
lyy+= (points[i].getY() - avgY)*(points[i].getY() - avgY);
lxy+= (points[i].getX() - avgX)*(points[i].getY() - avgY);
}
cout << "this line can be fitted by y=ax+b:" << endl;
cout << "a=" << lxy / lxx << ":";
cout << "b=" << avgY - lxy*avgX / lxx << ":";
return lxy / sqrt(lxx*lyy);
}
int main() {
Point p[10] = { Point(6,10),Point(14,20),Point(26,30),Point(75,80),Point(84,90),Point(33,40),Point(46,50),Point(54,60),Point(67,70),Point(100,100) };
float r = lineFit(p, 10);
cout << "line coefficient r=" << r << endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int x = 0, int y = 0) :x(x),y(y){}
int getX() { return x; }
int getY() { return y; }
friend float dist(Point&p1, Point&p2);
private:
int x, y;
};
float dist(Point&p1, Point&p2) {
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return sqrt(x*x + y*y);
}
int main() {
Point p1(1, 1);
Point p2(4, 5);
cout << dist(p1, p2) << endl;
}
#include <iostream>
using namespace std;
class Base1 {
public :
Base1(int i) { cout << "Constructing Basel" << i << endl; }
~Base1() {
cout << "Destructing Basel" << endl;
}
};
class Base2 {
public:
Base2(int j) { cout << "Constructing Base2" << j << endl; }
~Base2() {
cout << "Destructing Base2" << endl;
}
};
class Base3 {
public:
Base3() { cout << "Constructing Base3" << endl; }
~Base3() {
cout << "Destructing Base3" << endl;
}
};
class Derived :public Base2, public Base1, public Base3 {
public:
Derived(int a,int b,int c,int d):Base1(a),member2(d),member1(c),Base2(b){}
private:
Base1 member1;
Base2 member2;
Base3 member3;
};
//int fun(int x[10], int n) { return sizeof(x); }
int main() {
Derived obj(1, 2, 3, 4);
}
#include<iostream>
using namespace std;
//多继承同名函数
class B1 {
public:
int nV;
void fun() { cout << "Member of B1" << endl; }
};
class B2 {
public:
int nV;
void fun() { cout << "Member of B2" << endl; }
};
class D1: public B1, public B2{
public:
int nV;
void fun() { cout << "member of D1" << endl; }
};
int main() {
D1 d1;
d1.nV = 1;
d1.fun();
d1.B1::nV = 2;
d1.B1::fun();
d1.B2::nV = 3;
d1.B2::fun();
}