//例7-10 多继承的简单应用。
//Base1.h
#ifndef BASE1_H
#define BASE1_H
class Base1
{ public:
Base1(int x)
int getData() const
protected:
int value;
};
#endif
//Base2.h
#ifndef BASE2_H
#define BASE2_H
class Base2
{ public:
Base2(char c)
char getData() const
protected:
char letter;
};
#endif
//Derived.h
#ifndef DERIVED_H
#define DERIVED_H
class Derived : public Base1, public Base2 //公有继承Base1和Base2
{ friend ostream &operator<< ( ostream &, const Derived & ) ;
public :
Derived ( int, char, double ) ;
double getReal() const ;
private :
double real ;
};
Derived :: Derived ( int i, char c, double f ) //派生类的构造函数
: Base1( i ), Base2( c ), real( f )
double Derived::getReal() const
ostream &operator<< ( ostream &output, const Derived & d )
{ output << " Integer: " << d.value
<< "\n Character: " << d.letter
<< "\n Real number: " << d.real ;
return output ;
}
#endif
//ex7_10.cpp
#include
#include "Base1.h"
#include "Base2.h"
#include "Derived.h"
void main()
{ Base1 b1 ( 10 ) ;
Base2 b2 ( 'k' ) ;
Derived d ( 5, 'A', 2.5 ) ;
cout << "Object b1 contains integer " << b1.getData()
<< "\nObject b2 contains character " << b2.getData()
<< "\nObject d constains:\n" << d << "\n" ;
cout << "Data members of Derived can be accessed in other way:\n"
<<" Integer: "<< d.Base1::getData()
<<"\n Character: " << d.Base2::getData()
<< "\n Real number: " << d.getReal() << "\n" ;
return ;
}
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
//小程序也有100多行了,能多给点就多给点吧。 cout<
#include
using namespace std;
class matrix
{
public:
matrix(int a,int b);
~matrix() ;
matrix(const matrix &m);
int getmatrix();
void outputmatrix();
int lines;
int rows;
int **p;
};
matrix::matrix(int a,int b)
{
cout<<"构造了一个"< lines=a;
rows=b;
p=new int*[lines];
for(int i=0;i
}
matrix::matrix(const matrix &m)
{
cout<<"拷贝了一个"<
rows=m.rows;
p=new int*[lines];
for(int i=0;i
for(int i=0;i
}
int matrix::getmatrix()
{ cout<<"输入矩阵数据"<
if(cin) return 1;
else { cout<<"输入有误"<
void matrix::outputmatrix()
{
for(int i=0;i
for(int j=0;j
}
matrix::~matrix()
{
for(int i=0;i
}
}
matrix operator+(const matrix& mat1,const matrix& mat2)
{
if(mat1.lines!=mat2.lines||mat1.rows!=mat1.rows)
{
cout<<"相加的两个矩阵行数列数不等,返回+号左矩阵"<
}
else
{
matrix mat3(mat1);
for(int i=0;i
cout<<"调用+时临时变量将被释放"<
}
}
matrix operator-(const matrix& mat1,const matrix& mat2)
{
if(mat1.lines!=mat2.lines||mat1.rows!=mat1.rows)
{
cout<<"相加的两个矩阵行数列数不等,返回+号左矩阵"<
}
else
{
matrix mat3(mat1);
for(int i=0;i
cout<<"调用+时临时变量将被释放"<
}
}
int main()
{
int a,b;
cout<<"请输入矩阵行数和列数:"<
matrix A1(a,b),A2(a,b);
A1.getmatrix();
A2.getmatrix();
matrix *A3=new matrix(A1+A2);//因为没有定义复制操作符,这里一定要
//这样初始化A3,不然堆内存被释放后A3中二维数组将是悬垂。
//为了减号操作方便,采用指针,你题目给的pA1..等你可以采用我
//这样的自己添加
A3->outputmatrix();
A3=new matrix(A1-A2);
A3->outputmatrix();
system("pause");
return 0;
}
//这一点也不简单啊,调了一晚上,实现了基本功能。其实我也是菜鸟,很多问题简化了,没考虑
#include
using namespace std;
class matrix
{
private:
int lines;
int rows;
int *p;
public:
matrix(){};
matrix(int r, int l);
matrix(const matrix&);
friend matrix operator +(const matrix&, const matrix&);
friend matrix operator -(const matrix&, const matrix&);
friend ostream& operator <<(ostream&, const matrix&);
friend istream& operator >>(istream&, matrix&);
void operator =(const matrix&);
~matrix();
};
matrix::matrix(int r, int l)
{
lines = l;
rows = r;
p = new int[rows*lines];
}
matrix::matrix(const matrix& a)
{
rows = a.rows;
lines = a.lines;
p = a.p;
}
matrix::~matrix()
{
delete [] p;
}
matrix operator +(const matrix &a, const matrix &b)
{
matrix c(a.rows, a.lines);
for (int i = 0; i < c.rows*c.lines; i++)
{
*(c.p+i) = *(a.p+i) + *(b.p+i);
}
return c;
}
matrix operator -(const matrix &a, const matrix &b)
{
matrix c(a.rows, a.lines);
for (int i = 0; i < c.rows*c.lines; i++)
{
*(c.p+i) = *(a.p+i) - *(b.p+i);
}
return c;
}
ostream& operator <<(ostream& out, const matrix& a)
{
for (int i = 0; i < a.rows; i++)
{
for (int j = 0; j < a.lines; j++)
{
out << *(a.p+i*a.lines+j) << '\t';
}
out << endl;
}
return out;
}
istream& operator >>(istream& in, matrix& a)
{
int r, l;
cout << "请输入矩阵的行数:";
in >> r;
cout << "请输入矩阵的列数:";
in >> l;
a.rows = r;
a.lines = l;
a.p = new int[r*l];
cout << "请按从左到右、从上到下的顺序输入矩阵元素:";
for (int i = 0; i < a.rows; i++)
{
for (int j = 0; j < a.lines; j++)
{
in >> *(a.p+i*a.lines+j);
}
}
cout << "您输入的矩阵是:" << endl << a;
return in;
}
void matrix::operator =(const matrix& a)
{
lines = a.lines;
rows = a.rows;
p = new int[a.lines*a.rows];
for (int i = 0; i < lines*rows; i++)
{
*(p+i) = *(a.p+i);
}
}
int main()
{
matrix a, b, c;
cin >> a >> b;
c = a + b;
// c = a - b;
cout << c;
return 0;
}