c++-
来自「vc+环境下的面向对象编程.介绍了类的定义,封装性,继承性和多态性,以及模板的有」· 代码 · 共 770 行
TXT
770 行
实验1: 类和对象(4H)
//main.cpp
#include<iostream.h>
#include"student.h"
#include<iomanip.h>
const size=3;
void main()
{
int numberi;
char namei[20];
float score1,score2,score3;
student aSA[size];
for(int i=0;i<size;i++)
{
cout<<"please input the data of NO."<<i+1<<"student";
cout<<"\nnumber:";cin>>numberi;
cout<<"name:";cin>>namei;
cout<<"score of computer:";cin>>score1;
cout<<"score of english:";cin>>score2;
cout<<"score of mathematics:";cin>>score3;
aSA[i].modify(numberi,namei,score1,score2,score3);
}
cout<<"\n\n";
cout<<setw(8)<<"学号"<<setw(8)<<"姓名"<<setw(8)<<"计算机"
<<setw(8)<<"英语"<<setw(8)<<"数学"<<setw(8)<<"总分";
cout<<endl;
for(int j=0;j<size;j++)
aSA[j].print();
cout<<endl;
}
//score.cpp
#include<iostream.h>
#include"score.h"
#include<iomanip.h>
score::score()
{
computer=0;english=0;mathematics=0;
}
score::score(float x1,float y1,float z1)
{
computer=x1;english=y1;mathematics=z1;
}
float score::sum()
{
return (computer+english+mathematics);
}
void score::print()
{
cout<<setw(8)<<computer
<<setw(8)<<english
<<setw(8)<<mathematics
<<setw(8)<<sum();
}
void score::modify(float x2,float y2,float z2)
{
computer=x2;english=y2;mathematics=z2;
}
//student.cpp
#include<iostream.h>
#include"student.h"
#include<iomanip.h>
#include<string.h>
student::student():ascore()
{
number=0;
}
student::student(int number1,char* pname1,float score1,float score2,float score3):ascore(score1,score2,score3)
{
number=number1;
strncpy(name,pname1,sizeof(name));
name[sizeof(name)-1]='\0';
}
float student::sum()
{
return (ascore.sum());
}
void student::print()
{
cout<<endl;
cout<<setw(8)<<number
<<setw(8)<<name;
ascore.print();
}
void student::modify(int number2,char* pname2,float score21,float score22,float score23)
{
number=number2;
name=pname2;
ascore.modify(score21,score22,score23);
}
//score.h
class score
{
private:
float computer;
float english;
float mathematics;
public:
score();
score(float x1,float y1,float z1);
float sum();
void print();
void modify(float x2,float y2,float z2);
};
//student.h
#include"score.h"
class student
{
private:
int number;
char* name;
score ascore;
public:
student();
student(int number1,char* pname1,float score1,float score2,float score3);
float sum();
void print();
void modify(int number2,char* pname2,float score21,float score22,float score23);
};
class Location {
private:
int X,Y;
public:
int init(int x,int y)
{
X=x;
Y=y;
}
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
friend int distance(Location &a, Location &b);
}
int distance(Location &a, Location &b)
{ int dx,dy;
dx=a.X-b.X;
dy=a.Y-b.Y;
return sqrt(dx*dx+dy*dy);
}
void main(void)
{
Location p1(-50,50), p2(-50,-50)
int d=distance(p1,p2);
cout<<d;
}
********************************
class Location {
private:
int X,Y;
public:
int init(int x,int y)
{
X=x;
Y=y;
}
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
friend int distance(Location &a, Location &b);
}
int distance(Location *a, Location *b)
{ int dx,dy;
dx=a->X-b->X;
dy=a->Y-b->Y;
return sqrt(dx*dx+dy*dy);
}
void main(void)
{
Location p1(-50,50), p2(-50,-50)
Location * p1ptr=&p1 ;
Location * p2ptr=&p2 ;
int d=distance(p1ptr, p2ptr);
cout<<d;
}
class Location {
private:
int X,Y;
public:
int init(int x,int y)
{
X=x;
Y=y;
}
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
friend int distance(Location &a, Location &b);
}
int distance(Location &a, Location &b)
{ int dx,dy;
dx=a.X-b.X;
dy=a.Y-b.Y;
return sqrt(dx*dx+dy*dy);
}
void main(void)
{
Location p1(-50,50), p2(-50,-50)
Location & p1ptr=p1 ;
Location & p2ptr=p2 ;
int d=distance(p1ptr, p2ptr);
cout<<d;
}
class Rectangle {
Location Loc;
int H,W;
public:
void init(int x,int y,int h,int w)
{
Loc.init(x,y);
H=h;
W=w;
}
Location *GetLoc() { return &Loc; }
int GetH() { return H; }
int GetW() { return W; }
}
void main(void)
{
Rectangle r ;
r.init(50,50,20,40);
cout<<r. GetH();
cout<<r. GetW();
cout<<GetLoc()->GetX();
cout<<GetLoc()->GetY();
}
________________________________________
以上的init()可以使用构造函数完成。
________________________________________
试验2 继承性和多态性
#include<iostream.h>
struct node
{
int data;
node *next;
};
class linklist
{
private:
node *p,*q,*head;
int m;
public:
node *createlist(node *head);
virtual void print(node *head);
node *insert(node *head,int i,int x);
node *delete1(node *head,int i);
int length();
}
node linklist::*createlist(node *head)
{
p=new node;
q=new node;
cin>>p->data;
p->next=NULL;
whlie(p->data!=-1)
{
if(head==NULL)
head=q;
else
q->next=p;
q=p;
p=new node;
p->next=NULL;
cin>>p->data;
}
ruturn head;
}
void linklist::print(node *head)
{
p=head;
while(p!=NULL)
{
cout<<p->data<<endl;
p=p->next;
}
}
node linklist::*insert(node *head,int i,int x)
{
int k=1;
p=head;
while(p&&k<i-i)
{
p=p->next;
k++;
}
if(p==NULL||i<=0)
{
cout<<"插入位置不合理"<<endl;
return NULL;
}
q=new node;
q->data=x;
if(i==1)
{
q->next=head;
head=q;
}
else
{
q->next=p->next;
p->next=q;
}
return head;
}
node linklist::*delete1(node *head,int i)
{
int k=1;
p=head;
whlie(p&&k<i-1)
{
p=p->next;
k++;
}
if(p==NULL||p->next==NULL||i<=0)
{
cout<<"删除位置不合理"<<endl;
return NULL;
}
q=p->next;
if(i==1)
{
q=head;
head=q->next;
}
else
{
q=p->next;
p->next=q->next;
}
return head;
}
int linklist::length()
{
while(head!=NULL)
{
head=head->next;
m++;
}
cout<<"长度为"<<m<<endl;
return m;
}
class queue:public linklist
{
private:
node *p,*q,*head;
int m;
public:
void print(node *head);
node *insert(node *head,int i,int x);
node *delete1(node *head,int i);
}
node queue::*insert(node *head,int i,int x)
{
if(i>=2)
{
cout<<"插入位置不合理"<<endl;
return NULL;
}
else
{
q=new node;
q->data=x;
q->next=head;
head=q;
}
return head;
}
node queue::*delete1(node *head,int i)
{
int k=1;
p=head;
whlie(p&&k<i-1)
{
p=p->next;
k++;
}
if(i!=m)
{
cout<<"删除位置不合理"<<endl;
return NULL;
}
q=p->next;
if(i==1)
{
q=head;
head=q->next;
}
else
{
q=p->nest;
p->next=q->next;
}
return head;
}
class stack:public linklist
{
private:
node *p,*q,*head;
int m;
public:
void print(node *head);
node *insert(node *head,int i,int x);
node *delete1(node *head,int i);
}
node stack::*insert(node *head,int i,int x)
{
int k=1;
p=head;
while(p&&k<i-i)
{
p=p->next;
k++;
}
if(p==NULL||i<m)
{
cout<<"插入位置不合理"<<endl;
return NULL;
}
q=new node;
q->data=x;
if(i==m)
{
q->next=p->next;
p->next=q;
}
return head;
}
node stack::*delete1(node *head,int i)
{
int k=1;
p=head;
whlie(p&&k<i-1)
{
p=p->next;
k++;
}
if(i!=m)
{
cout<<"删除位置不合理"<<endl;
return NULL;
}
else
{
q=p->next;
q=p->next;
p->next=q->next;
}
return head;
}
void mian()
{
int m1,m2,m3;
linklist *lone;
queue qone;
stack sone;
lone->createlist(node *head);
lone->print(node *head);
lone->insert(node *head,int i,int x);
lone->delete1(node *head,int i);
lone->length();
m1=lone->length();
qone.createlist(node *head);
qone.print(node *head);
qone.insert(node *head,int i,int x);
qone.delete1(node *head,int i);
qone.length();
lone=&qone;
m2=lone->length();
sone.createlist(node *head);
sone.print(node *head);
sone.insert(node *head,int i,int x);
sone.delete1(node *head,int i);
sone.length();
lone=&sone;
m3=lone->length();
cout<<m1<<endl;
cout<<m2<<endl;
cout<<m3<<endl;
}
*************************************
#include<iostream>
using namespace std;
class complex
{
private:
int real;
int imag;
public:
complex(int r=0,int i=0)
{
real=r;
imag=i;
}
complex operator+(complex &c);
complex operator-(complex &c);
friend complex operator+(complex &c1,complex &c2);
friend complex operator-(complex &c1,complex &c2);
void print();
}
complex complex::operator+(complex &c)
{
complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
complex complex::operator-(complex &c)
{
complex temp;
temp.real=real-c.real;
temp.imag=imag-c.imag;
return temp;
}
complex operator+(complex &c1,complex &c2)
{
return(c1.real+c2.real,c1.imag+c2.imag);
}
complex operator-(complex &c1,complex &c2)
{
return(c1.real-c2.real,c1.imag-c2.imag);
}
void complex::print()
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}
void main()
{
complex m1(1,1),m2(2,2),m3;
m3=m1+m2;
m3.print();
}
-------------------
//用模板函数实现10个数从大到小的排序
#include <iostream>
using namespace std;
#define size 10
template<class T>
class base
{
private:
int m,n;
T array[size];
public:
base();
base(T a,T b,T c,T d,T e,T f,T g,T h,T i,T j);
void sort();
void display();
};
template<class T>
void sortone(T a,T b,T c,T d,T e,T f,T g,T h,T i,T j)
{
T array[size];
array[0]=a;
array[1]=b;
array[2]=c;
array[3]=d;
array[4]=e;
array[5]=f;
array[6]=g;
array[7]=h;
array[8]=i;
array[9]=j;
T temp;
int m,n;
for(m=0;m<size-1;m++)
{
for(n=m+1;n<size;n++)
{
if(array[m]<array[n])
{
temp=array[m];
array[m]=array[n];
array[n]=temp;
}
}
}
for(m=0;m<=size-1;m++)
{
cout<<"array"<<"["<<m<<"]"<<":"<<array[m]<<endl;
}
}
template<class T>
base<T>::base(T a,T b,T c,T d,T e,T f,T g,T h,T i,T j)
{
array[0]=a;
array[1]=b;
array[2]=c;
array[3]=d;
array[4]=e;
array[5]=f;
array[6]=g;
array[7]=h;
array[8]=i;
array[9]=j;
}
template<class T>
void base<T>::sort()
{
T temp;
for(m=0;m<size-1;m++)
{
for(n=m+1;n<size;n++)
{
if(array[m]<array[n])
{
temp=array[m];
array[m]=array[n];
array[n]=temp;
}
}
}
}
template<class T>
void base<T>::display()
{
for(m=0;m<=size-1;m++)
{
cout<<"array"<<"["<<m<<"]"<<":"<<array[m]<<endl;
}
}
void main()
{
int a[size];
int m;
cout<<"请输入10个整形数字"<<endl;
for(m=0;m<10;m++)
{
cin>>a[m];
}
cout<<"通过类模板实现的如下所示"<<endl;
base<int> bone(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
bone.sort();
bone.display();
cout<<"通过函数模板实现的如下所示"<<endl;
sortone(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
}
*********************
//文件读写
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
ofstream table;
table.open("sqrtable.txt");
int i,j;
if(!table)
{
cout<<"Cannot open output file!\n";
return 1;
}
table<<"|";
for(i=0;i<10;i++)
table<<setw(5)<<i<<" ";
table<<endl;
table<<"---+";
for(i=0;i<10;i++)
table<<"--------";
table<<fixed<<setprecision(4);
for(i=0;i<10;i++)
{
table<<endl;
table<<setw(2)<<i<<"|";
for(j=0;j<10;j++)
table<<setw(8)<<sqrt(10*i+j);
}
table.close();
return 0;
}
//文件拷贝
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char c;
ifstream fin("sqrtable.txt");
if(!fin)
{
cout<<"Cannot open output file!\n";
return 1;
}
ofstream fout("mttable.txt");
if(!fout)
{
cout<<"Cannot open output file!\n";
return 1;
}
while(fin.get(c))
fout.put(c);
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?