C++程序的多文件组成 如何将一个源程序分成三个文件 最好举例说明 我不明白为什么我分成后出现

C++程序的多文件组成 如何将一个源程序分成三个文件 最好举例说明 我不明白为什么我分成后出现,第1张

把一个源程序分成三个文件,一般情况包含《文件名》h头文件用于实现函数的声明,函数文件<文件名>cpp,主函数文件头文件和函数文件用#include“文件名h(cpp)开头声明!

下边例子头文件headh是类定义头文件,headcpp是类实现文件4-4cpp是主函数文件

//headh

class Point{

public:

Point(int xx=0,int yy=0){

x=xx;

y=yy;

}

Point(Point &p);

int getX(){return x;}

int getY(){return y;}

private:

int x,y;

};class Line{

public:

Line(Point xp1,Point xp2);

Line(Line &l);

double getLen(){return len;}

private:

Point p1,p2;

double len;

};

//headapp

#include"headh"

#include<iostream>

#include<cmath>

using namespace std;

Point::Point(Point &p){

x=px;

y=py;

cout<<"Calling the copy constructor of Point"<<endl;

}

Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){

cout<<"Calling consturctor of Line"<<endl;

double x=static_cast<double>(p1getX()-p2getX());

double y=static_cast<double>(p1getY()-p2getY());

len=sqrt(xx+yy);

}

Line::Line (Line &l):p1(lp1),p2(lp2){

cout<<"Calling the copy constructor of Line"<<endl;

len=llen ;

}

//4_4app

#include "headh"

#include <iostream>

using namespace std;

int main(){

Point myp1(1,1),myp2(4,5);

Line line(myp1,myp2);

Line line2(line);

cout<<"The length of the line2 is:";

cout<<linegetLen()<<endl;

cout<<"The length of the line2 is:";

cout<<line2getLen()<<endl;

return 0;

}

综合到一块如下:

#include <iostream>

using namespace std;

class Point{

public:

Point(int xx=0,int yy=0){

x=xx;

y=yy;

}

Point(Point &p);

int getX(){return x;}

int getY(){return y;}

private:

int x,y;

};class Line{

public:

Line(Point xp1,Point xp2);

Line(Line &l);

double getLen(){return len;}

private:

Point p1,p2;

double len;

};using namespace std;

Point::Point(Point &p){

x=px;

y=py;

cout<<"Calling the copy constructor of Point"<<endl;

}

Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){

cout<<"Calling consturctor of Line"<<endl;

double x=static_cast<double>(p1getX()-p2getX());

double y=static_cast<double>(p1getY()-p2getY());

len=sqrt(xx+yy);

}

Line::Line (Line &l):p1(lp1),p2(lp2){

cout<<"Calling the copy constructor of Line"<<endl;

len=llen ;

}

int main(){

Point myp1(1,1),myp2(4,5);

Line line(myp1,myp2);

Line line2(line);

cout<<"The length of the line2 is:";

cout<<linegetLen()<<endl;

cout<<"The length of the line2 is:";

cout<<line2getLen()<<endl;

return 0;

}

#include<iostreamh>

class A

{ public:

A(int a)

{ x=a;

cout<<"调用类A构造函数"<<endl;

}

~A(){};

int getX(){return x;}

protected:

int x;

}; 

class B:virtual public A

{ public:

B(int b ):A(b) //将此处补充完整

{cout<<"调用类B构造函数"<<endl;}

~B( ){};

};

class C: public virtual A

{public:

C(int c ):A(c) //将此处补充完整

{cout<<"调用类C构造函数"<<endl;}

~C( ){};

};

class D:public B,public C

{ public:

D(int d):A(d),B(d),C(d)

//此处将D类的构造函数的定义补充完成

{cout<<"调用类D构造函数"<<endl;}

~D( ){};

};

int main()

{ D d(3);

cout<<"X="<<dgetX()<<endl;

}

输出结果

调用类A构造函数

调用类B构造函数

调用类C构造函数

调用类D构造函数

X=3

#include<iostream>

#include<mathh>

using namespace std;

class Point {

public:

 Point() {x = 0; y = 0; }

 Point(double xv,double yv) {x = xv;y = yv;}

 Point(Point& pt) { x = ptx; y = pty; }

 double getx() { return x; }

 double gety() { return y; }

 double Area() { return 0; }

 void Show() { cout<<"x="<<x<<' '<<"y="<<y<<endl; }

private:

 double x,y;

};

class Rectangle:public Point{

 double a,b;

 double l;

 double w;

 public:

  Rectangle(double aa,double bb,double ll,double ww):Point(aa,bb),a(aa),b(bb),l(ll),w(ww){

  }

  void position(Point& pt){

   if(ptgetx()>a && ptgetx()<a+l && ptgety()>b-w && ptgety()<b) cout<<"The point is in the rectangle!"<<endl;

   else if((ptgetx()==a &&(ptgety()>=b-w && ptgety()<=b)) || (ptgetx()==a+l &&(ptgety()>=b-w && ptgety()<=b)) || (ptgety()==b && (ptgetx()>=a && ptgetx()<=a+l)) || (ptgety()==b-w && (ptgetx()>=a && ptgetx()<=a+l))){

    cout<<"The point is at the edge of the rectangle!"<<endl;

   }

   else cout<<"The point is out of the rectangle!"<<endl;

  }

  double Area(){

   return lw;

  }

};

class Circle:public Point{

 double a,b;

 double r;

 public:

  Circle(double aa,double bb,double rr):Point(aa,bb),a(aa),b(bb),r(rr){

  }

  void position(Point& pt){

   double d=sqrt((ptgetx()-a)(ptgetx()-a)+(ptgety()-b)(ptgety()-b));

   if(d<r) cout<<"The point is in the circle!"<<endl;

   else if (d==r) cout<<"The point is on the circle!"<<endl;

   else cout<<"The point is out of the circle!"<<endl;

  }

  double Area(){

   return 314rr;

  }

};

//TEST!!!

/

int main(){

 Point p(1,2);

 Rectangle r(0,2,4,2);

 cout<<"The area of rectangle: "<<rArea()<<endl;

 rposition(p);

 Circle c(0,2,1);

 cout<<"The area of circle: "<<cArea()<<endl;

 cposition(p);

}

/

#include <iostream>

#include______ //看见下面的fstream没 所以这里是#include <QStream>

using namespace std;

void main( )

{

fstream fin,fout;

fout.open("my.txt",ios::out); //打开文件

if(! fout.is_open( ))

return;

for(int i=0;i<3;i=i+1) //输出line的号

fout<<"This is line"<<i+1<<endl;

fout.close( );

fin.open("my.txt",ios::in); //打开文件

if(! fin.is_open( ))

return;

char str[100];

while(______) /把文件东西读入到str知道文件结束 所以 判断文件结束的一个东东忘记是什么函数了,哥又不是百度,哥可以查msdn至此,后面不看了,出这道题的人傻逼,肯定不是一个公司程序员!/

{

fin.getline(str,100);

cout<<str<<endl;

}

fin.close( );

}

50.求两个浮点数之差的cha函数的原型声明、调用方法。

#include <iostream>

using namespace std;

void main( )

{

float a,b;

______;∥函数cha的原型声明 float cha( float x, float y );

a=12.5;

b=6.5;

float c=__________;∥调用函数cha cha( a, b );

cout<<c<<endl;

}

float cha(float x,float y)

{

float w;

w=x-y;

return w;

}

后面的不看了,上面两道题第一道题够二,第二道题你自己看书就能解决

以上就是关于C++程序的多文件组成 如何将一个源程序分成三个文件 最好举例说明 我不明白为什么我分成后出现全部的内容,包括:C++程序的多文件组成 如何将一个源程序分成三个文件 最好举例说明 我不明白为什么我分成后出现、请将下面程序中不完整的地方补充完整,并调试运行结果、编写程序,以点point类为基类,派生出矩形类Rectangle和圆类Circle。矩形由左上角的顶点和长等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10078528.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存