目录
1.打印数字字符
#include <iostream>
#include <iomanip>
using namespace std;
class datatype
{
public:
datatype()
{
}
datatype(char c):c(c),i(0),f(0)
{
}
datatype(int i):c(0),i(i),f(0)
{
}
datatype(float f):c(0),i(0),f(f)
{
}
void setC(char c)
{
this->c=c;
}
void setI(int i)
{
this->i=i;
}
void setF(float f)
{
this->f=f;
}
char getC()
{
return c;
}
int getI()
{
return i;
}
float getF()
{
cout << fixed << setprecision(2);
return f;
}
void print()
{
if(getC())
cout << "character:" << getC() << endl ;
if(getI())
cout << "integer:" << getI() << endl ;
if(getF())
cout << "floating point:" << getF() << endl;
}
private:
char c;
int i;
float f;
};
int main()
{
char c;
int i;
float f;
cin>>c>>i>>f;
datatype A(c), B(i), C(f);
A.print();
B.print();
C.print();
}
2.打印面积(类的继承)
#include<iostream>
#include<iomanip>
using namespace std;
class Area_cl
{
public:
Area_cl(){};
virtual ~Area_cl(){};
void setHeight(double h)
{
height=h;
}
void setWidth(double w)
{
width=w;
}
double getHeight()
{
return height;
}
double getWidth()
{
return width;
}
virtual double area(){};//别忘了{}
virtual void showArea(){};
private:
double height;
double width;
};
class Rectangle:public Area_cl
{
public:
Rectangle(){};
Rectangle(double a,double b)
{
setHeight(a);
setWidth(b);
}
~Rectangle()
{
}
double area()
{
cout << fixed << setprecision(2);
return getHeight()*getWidth();
}
void showArea()
{
cout << "the rectangle's area is " << area() << endl;
}
};
class Isosceles:public Area_cl
{
public:
Isosceles(){};
Isosceles(double h,double w)
{
setHeight(h);
setWidth(w);
}
~Isosceles()
{
}
double area()
{
cout << fixed << setprecision(2);
return getHeight()*getWidth()/2;
}
void showArea()
{
cout << "the Isosceles's area is " << area() << endl;
}
};
int main()
{
Area_cl* p;
double r,s;
cin>>r>>s;
Rectangle b(r,s);
cin>>r>>s;
Isosceles i(r,s);
p=&b;
p->showArea();
p= &i;
p->showArea();
return 0;
}
3.简单的矩阵加减
/**
*注意流输入和流提取的区别:
*对于输出操作符 (<<) 加上 const:
*(即 const Matrix &m),
*意味着你在保证这个操作不会修改传递进来的矩阵对象。
*这对于输出操作符是合理的,因为你只是读取对象的信息并打印出来,并不需要改变对象的状态。
*对于输入操作符 (>>) 不加 const:
*(即 Matrix &m 而非 const Matrix &m)。
*这是因为输入操作符的目的是从输入流中读取数据来填充对象,这一过程通常涉及修改对象的成员变量。
*/
#include <iostream>
#include <string>
using namespace std;
class Matrix
{
friend Matrix operator+(const Matrix & m1,const Matrix & m2);
friend Matrix operator-(const Matrix & m1,const Matrix & m2);
friend ostream& operator<< (ostream& ,const Matrix& );
friend istream& operator >> (istream& ,Matrix& );
public:
Matrix(){};
Matrix(int i,int j)
{
setI(i);
setJ(j);
}
~Matrix(){};
void setI(int i)
{
this->i=i;
}
void setJ(int j)
{
this->j=j;
}
int getI()
{
return i;
}
int getJ()
{
return j;
}
private:
int i;
int j;
int data[10][10];
};
Matrix operator+(const Matrix & m1,const Matrix & m2)
{
Matrix m(m1.i,m1.j);
for(int l=0;l<m.i;l++)
{
for(int k=0;k<m.j;k++)
{
m.data[l][k]=m1.data[l][k]+m2.data[l][k];
}
}
return m;
}
Matrix operator-(const Matrix & m1,const Matrix & m2)
{
Matrix m(m1.i,m1.j);
for(int l=0;l<m.i;l++)
{
for(int k=0;k<m.j;k++)
{
m.data[l][k]=m1.data[l][k]-m2.data[l][k];
}
}
return m;
}
ostream& operator<< (ostream& os ,const Matrix& m)
{
for(int l=0;l<m.i;++l)
{
for(int k=0;k<m.j;++k)
{
os << m.data[l][k] << " ";
}
os << endl;
}
return os;
}
istream& operator>> (istream& is,Matrix& m)
{
for(int l=0;l<m.i;++l)
{
for(int k=0;k<m.j;++k)
{
is >> m.data[l][k] ;
}
}
return is;
}
int main()
{
int i,j;
cin >> i >> j;
Matrix m1(i,j),m2(i,j);
cin>>m1;
cin>>m2;
cout << "m1 + m2 :" << endl ;
cout <<(m1+m2);
cout << "m1 - m2 :" << endl ;
cout <<(m1-m2);
return 0;
}
标签:const,Matrix,int,double,void,打印,加减,return,20240521
From: https://blog.csdn.net/2301_80202274/article/details/139104125