惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Securelist
C
Cybersecurity and Infrastructure Security Agency CISA
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security Affairs
Hacker News: Ask HN
Hacker News: Ask HN
L
Lohrmann on Cybersecurity
PCI Perspectives
PCI Perspectives
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
月光博客
月光博客
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
The GitHub Blog
The GitHub Blog
Webroot Blog
Webroot Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
F
Full Disclosure
U
Unit 42
Jina AI
Jina AI
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
T
Troy Hunt's Blog
腾讯CDC
T
Threatpost
H
Hacker News: Front Page
P
Palo Alto Networks Blog
博客园 - 聂微东
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
Help Net Security
Help Net Security
L
LINUX DO - 热门话题
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Spread Privacy
Spread Privacy

博客园 - 逍遥子

MSN空间上的以往技术贴整理 Link Java to Maple - 逍遥子 C++的多态的被覆盖的问题 类引用机制 - 逍遥子 - 博客园 防止盗链的简单技术 验证码(CAPTCHA)的破解技术 校内网狗狗刷骨机完全版(不再维护) - 逍遥子 - 博客园 HTTP外挂的简单实现 C++的中英文字符串表示(string,wstring) C++学习之2--10.09题目答案 C++学习之一--基本介绍 下面内容为C++的教学 VC6的C++标准支持不完全 XML基础学习1 数论中求公因子、求模逆的算法 QuickSort和MergeSort算法及其效率比较源代码 数据库时间段分组查询解决方法和数据转储方法 Java 通配符匹配查找文件 动态加载Java运行环境和运行实例
继承、重载运算符、虚函数与向量、矩阵运算
逍遥子 · 2008-06-12 · via 博客园 - 逍遥子

这是个关于如何使用继承、重载运算符、虚函数以实现向量和矩阵运算的例子。是我学习时写的,供大家学习C++基本知识使用。

#include <math.h>
#include 
<iostream.h>

void error(){
    cout
<<"error!"<<endl;
    }
;
//=============================================================
class vm
{
public:
    vm();
    
virtual ~vm();
    
void set();
    
void print();
    
}
;


class Vec: public vm                            //继承
{
private:
    
double *a;
    
public:
    
int sv;
    Vec()
    
{   a=new double[1];
        sv
=1;
    }

    
void set()
    
{
        cout
<<"N=";
        cin
>>sv;
        a
=new double[sv];
        
for(int i=0;i<sv;i++)
        
{
            cout
<<"a["<<i+1<<"]=";
            cin
>>*(a+i);
            cout
<<endl;
        }
;
    }
;
    
void set(double * v)
    
{
        
for(int i=0;i<sv;i++)
            
*(a+i)=*(v+i);
    }


    
~Vec(){
        delete[]a;
    }
;

    
double &operator[](int i)
    
{
        
if(i<1||i>sv)error();
        
return a[i-1];
    }
;
    
void print(){
        
for(int i=0;i<sv;i++)
            cout
<<"a["<<i+1<<"]="<<a[i]<<endl;
                }
;

    
void operator -(){
        
for(int i=0;i<this->sv;i++)
            
*(a+i)=(-(*(a+i)));
    }
;
    Vec 
&operator=(Vec &m){
        a
=new double[m.sv];
        
for(int i=0;i<m.sv;i++)
            a[i]
=m.a[i];
    }
;
}
;

int operator!=(Vec &v1,Vec &v2){
    
if(v1.sv!=v2.sv){return 0;}
    
else
        
for(int i=1;i<=v1.sv;i++)
            
if( v1[i] != v2[i] )return 0;
            
else
                
return 1;
}


Vec 
operator+(Vec &m1,Vec &m2){            //加号运算符
    if(m1.sv!=m2.sv)error();
    Vec temp;
    
for(int i=1;i<=m1.sv;i++)temp[i]=m1[i]-m2[i];
    
return temp;
}
;
Vec 
operator-(Vec &m1,Vec &m2){            //减号运算符
    if(m1.sv!=m2.sv)error();
    Vec temp;
    
for(int i=1;i<=m1.sv;i++)temp[i]=m1[i]-m2[i];
    
return temp;
}
;
Vec 
operator*(Vec &m1,int &m2){        //乘号运算符
    Vec temp;
    
for(int i=1;i<=m1.sv;i++)
        temp[i]
=m1[i]*m2;
    
return temp;
}
;
Vec 
operator*(int &m2,Vec &m1){
    Vec temp;
    
for(int i=1;i<m1.sv;i++)
        temp[i]
=m1[i]*m2;
    
return temp;
}
;

class Matrix:public vm                                    //继承
{
private:
    
double **m;
    
public:
    
int si,sj;
    
~Matrix(){
        delete[]m;
    }

    Matrix(
int sii=2,int sjj=2);
    Matrix MatrixI(
int tt)
    
{
        Matrix temp(tt,tt);
        
for(int i=0;i<tt;i++)
            temp[i][i]
=1;
        
return temp;
    }

    Matrix(Vec 
&v)
    
{
        
*m=new double[v.sv];
        
for(int i=1;i<=v.sv;i++)
            m[
0][i-1]=v[i];
            
    }

    
void set(double *mm)
    
{
        
for(int i=0;i<si;i++)
            
for(int j=0;j<sj;j++)
                m[i][j]
=mm[i*j+j];
    }


    Vec Matrix::
operator[](int i){
        
if((i<1)&&(i>si))error();
        Vec temp;
        
for(int j=0;j<si;j++)
            temp[j
+1]=m[i][j];
        
return temp;
    }

    Matrix 
operator=(const Matrix &m1)            //赋值运算符
    {
        Matrix m2(m1.si,m1.sj);
        
for(int i=1;i<=m1.si;i++)
            m2[i]
=m1[i];
        
return m2;
    }
;
    Matrix 
operator-()                            //负号运算符
    {
        
for(int i=0;i<si;i++)
            
for(int j=0;j<sj;j++)
                m[i][j]
=(-m[i][j]);
    }
;
}
;

Matrix::Matrix(
int si,int sj)
{
    
for(int j=0;j<sj;j++)
        
for(int i=0;i<si;i++)
            m[i][j]
=0;
}



    
int operator!=(Matrix &m1,Matrix &m2)
    
{
        
if ((m1.si!=m2.si)||(m1.sj!=m1.sj))
        
{return 0;}
        
else
            
for(int i=1;i<=m1.si;i++)
                
if(m1[i]!=m2[i]){
                
return 0;}

                
else
                    
return 1;
    }


    Matrix 
operator+(Matrix &m1,Matrix &m2)    //加号运算符
    {
        
if((m1.si!=m2.si)||(m1.sj!=m2.sj))error();
        Matrix temp;
        
for(int i=1;i<=m1.si;i++)
        temp[i]
=m1[i]+m2[i];
        
return temp;
    }


    Matrix 
operator-(Matrix &m1,Matrix &m2)        //减号运算符
    {
        
if((m1.si!=m2.si)||(m1.sj!=m2.sj))error();
        Matrix temp;
        
for(int i=1;i<=m1.si;i++)
        temp[i]
=m1[i]-m2[i];
        
return temp;
    }

    Matrix 
operator *(Matrix& m1,Matrix& m2);    //乘号运算符
    {
        
if(m1.sj!=m2.si)error();
        Matrix temp(m1.si,m2.sj);
        
for(int j=1;j<=m2.sj;j++)
            
for(int i=1;i<=m1.si;i++)
                
for(int k=1;k<=m1.j;k++)
                    (temp[i])[j]
=(temp[i])[j]+(m1[i])[k]*(m2[k])[j];
        
return temp;    
    }

        Matrix 
operator * (Matrix& m1,int m2);
    
{
        Matrix temp(m1.si,m1.sj);
        
for(int i=1;i<=m1.si;i++)
            
for(int j=1;j<=m1.sj;j++)
                (temp[i])[j]
=(m1[i])[j]*m2;
            
return temp;
    }


    Matrix 
operator ^ (Matrix &m1,int m2);//幂运算符号
    Matrix operator / (Matrix &m1,int &n);//除号运算符

void main()
{
   Vec v1,v2;
   Matrix m1,m2;
   v1.
set();
   v1.print();
   
double t[4]={1.234,2.322,1.7665,5.231};
   v2.
set(t);
   v2.print();
   v1
=v2*3;
   v1.print();

}