C++类的实例:一个向量类(Vector)

简介: C++类的实例:一个向量类(Vector)

背景知识:

在数学中,向量(也称为欧几里得向量、几何向量、矢量),指具有大小(magnitude)和方向的量.

百度百科--向量

本例实现一个二维向量。二维向量通常表示的方法有 直角坐标(x,y)和极坐标(a,m)   // (a为长度,m为角度)。

创建一个类首先考虑类包含的数据。

一个向量(Vector)可以使用两个double表示直角坐标的x,y;用两个double表示极坐标的a,m。

还需要则有:

  public:
    enum Mode{RECT,POL};
private:
    double x;
    double y;
    double mag;
    double ang;
    Mode mode;

接着考虑对向量的操作,一个类最基本的 应该有构造函数和析构函数。

在创建构造函数时,我们遇到了一个问题,由于同一个向量有两个形式,那么创建时应该怎么做呢?

最容易想到的办法就是选择其一,如直角坐标。那么,构造函数应该像是这种形式,

 Vector(double n1, double n2);

然后让x=n1,y=n2;这样就可以设置向量的直角坐标,但是,极坐标就被忽略了。

考虑到向量的直角坐标和极坐标是一一对应的,于是想让程序自动设置对应的极坐标。

在Vector类中添加函数(在private中,因为不需要给外部提供接口)

    void set_mag();
    void set_ang();

具体实现为:

  void Vector::set_mag()
  {
    mag = sqrt(x*x + y * y);
  }
  void Vector::set_ang()
  {
    if (x == 0.0 && y == 0.0)
      ang = 0.0;
    else
      ang = atan2(y, x);
  }

然而,如果用户想要用极坐标形式输入向量,当前的构造函数无法满足需求。

于是,改造构造函数,添加一个参数 form 用来选择形式。

Vector(double n1, double n2, Mode form= RECT);

相应的,构造函数的实现改为:

  Vector::Vector(double n1, double n2, Mode form )
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
      set_mag();
      set_ang();
    }
    else if (form == POL)
    {
      mag = n1;
      ang = n2/Rad_to_deg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Incorrect 3rd argument to Vector() --";
      cout << "Vector set to 0";
      x = y = mag = ang = 0;
      mode = RECT;
    }
  }

相应的,在Vector类添加set_x();set_t()方法。

  void set_x();
  void set_y();
  void Vector::set_x()
  {
    x = mag * cos(ang);
  }
  void Vector::set_y()
  {
    y = mag * sin(ang);
  }

如此,完成了构造函数。


析构函数使用默认的即可。或者自己加上一个构造函数        ~Vector(); 然后在对应的实现函数函数体中留下空格即可。


                                                                                               Vector::~Vector()   { }

完成了构造函数和析构函数,考虑向量需要的其他功能/接口。

两个向量相加减,向量的数乘,改变向量的坐标。输出向量。

获取向量的坐标值(提供接口才可访问,否则在private中不能访问)

设置向量的形式

......

具体如下:

    void reset(double n1, double n2, Mode form = RECT);
    double xval()const { return x; }
    double yval()const { return y; }
    double magval()const { return mag; }
    double angval()const { return ang; }
    void polar_mode();
    void rect_mode();
    //operator
    Vector operator+(const Vector&b)const;
    Vector operator-(const Vector&b)const;
    Vector operator-()const;
    Vector operator*(double n)const;
    //friends
    friend Vector operator*(double n, const Vector &a);
    friend std::ostream & operator<<(std::ostream &os, const Vector &v);


实现:

  void Vector::reset(double n1, double n2, Mode form )
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
      set_mag();
      set_ang();
    }
    else if (form == POL)
    {
      mag = n1;
      ang = n2 / Rad_to_deg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Incorrect 3rd argument to Vector() --";
      cout << "Vector set to 0";
      x = y = mag = ang = 0;
      mode = RECT;
    }
  }
 
  void Vector::polar_mode()
  {
    mode = POL;
  }
  void Vector::rect_mode()
  {
    mode = RECT;
  }
  //operator
  Vector Vector::operator+(const Vector&b)const
  {
    return Vector(x + b.x, y + b.y );
  }
  Vector Vector::operator-(const Vector&b)const
  {
    return Vector(x - b.x, y - b.y);
  }
  Vector Vector::operator-()const
  {
    return Vector(-x, -y);
  }
  Vector Vector::operator*(double n)const
  {
    return Vector(x*n, y*n);
  }
  //friends
  Vector operator*(double n, const Vector &a)
  {
    return a * n;
  }
  std::ostream & operator<<(std::ostream &os, const Vector &v)
  {
    if (v.mode == Vector::RECT)
      os << "(x,y) = (" << v.x << ", " << v.y << ")";
    else if (v.mode == Vector::POL)
    {
      os << "(m,a) = (" << v.mag << ", " << v.ang*Rad_to_deg << ")";
    }
    else
      os << "Vector Object mode is invalid";
    return os;
  }

至此已经完成了Vector类的定义。(借助了cmath库进行了坐标两种形式转换)

完整代码:

//vector.h
#pragma once
#include<iostream>
namespace VECTOR
{
  class Vector
  {
  public:
    enum Mode{RECT,POL};
  private:
    double x;
    double y;
    double mag;
    double ang;
    Mode mode;
    void set_mag();
    void set_ang();
    void set_x();
    void set_y();
  public:
    Vector();
    Vector(double n1, double n2, Mode form= RECT);
    ~Vector();
    void reset(double n1, double n2, Mode form = RECT);
    double xval()const { return x; }
    double yval()const { return y; }
    double magval()const { return mag; }
    double angval()const { return ang; }
    void polar_mode();
    void rect_mode();
    //operator
    Vector operator+(const Vector&b)const;
    Vector operator-(const Vector&b)const;
    Vector operator-()const;
    Vector operator*(double n)const;
    //friends
    friend Vector operator*(double n, const Vector &a);
    friend std::ostream & operator<<(std::ostream &os, const Vector &v);
  };
}


#include<cmath>
#include"vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;
 
namespace VECTOR
{
  const double Rad_to_deg = 45.0 / atan(1.0);
 
  //private methods
  void Vector::set_mag()
  {
    mag = sqrt(x*x + y * y);
  }
  void Vector::set_ang()
  {
    if (x == 0.0 && y == 0.0)
      ang = 0.0;
    else
      ang = atan2(y, x);
  }
  void Vector::set_x()
  {
    x = mag * cos(ang);
  }
  void Vector::set_y()
  {
    y = mag * sin(ang);
  }
  //public methods
  Vector::Vector()
  {
    x = y = mag = ang = 0.0;
    mode = RECT;
  }
  Vector::Vector(double n1, double n2, Mode form )
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
      set_mag();
      set_ang();
    }
    else if (form == POL)
    {
      mag = n1;
      ang = n2/Rad_to_deg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Incorrect 3rd argument to Vector() --";
      cout << "Vector set to 0";
      x = y = mag = ang = 0;
      mode = RECT;
    }
  }
  Vector::~Vector()
  {
  }
  void Vector::reset(double n1, double n2, Mode form )
  {
    mode = form;
    if (form == RECT)
    {
      x = n1;
      y = n2;
      set_mag();
      set_ang();
    }
    else if (form == POL)
    {
      mag = n1;
      ang = n2 / Rad_to_deg;
      set_x();
      set_y();
    }
    else
    {
      cout << "Incorrect 3rd argument to Vector() --";
      cout << "Vector set to 0";
      x = y = mag = ang = 0;
      mode = RECT;
    }
  }
 
  void Vector::polar_mode()
  {
    mode = POL;
  }
  void Vector::rect_mode()
  {
    mode = RECT;
  }
  //operator
  Vector Vector::operator+(const Vector&b)const
  {
    return Vector(x + b.x, y + b.y );
  }
  Vector Vector::operator-(const Vector&b)const
  {
    return Vector(x - b.x, y - b.y);
  }
  Vector Vector::operator-()const
  {
    return Vector(-x, -y);
  }
  Vector Vector::operator*(double n)const
  {
    return Vector(x*n, y*n);
  }
  //friends
  Vector operator*(double n, const Vector &a)
  {
    return a * n;
  }
  std::ostream & operator<<(std::ostream &os, const Vector &v)
  {
    if (v.mode == Vector::RECT)
      os << "(x,y) = (" << v.x << ", " << v.y << ")";
    else if (v.mode == Vector::POL)
    {
      os << "(m,a) = (" << v.mag << ", " << v.ang*Rad_to_deg << ")";
    }
    else
      os << "Vector Object mode is invalid";
    return os;
  }
}
 


相关文章
|
2月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
2天前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
32 12
|
1月前
|
设计模式 安全 C++
【C++进阶】特殊类设计 && 单例模式
通过对特殊类设计和单例模式的深入探讨,我们可以更好地设计和实现复杂的C++程序。特殊类设计提高了代码的安全性和可维护性,而单例模式则确保类的唯一实例性和全局访问性。理解并掌握这些高级设计技巧,对于提升C++编程水平至关重要。
48 16
|
1月前
|
编译器 C++
类和对象(中 )C++
本文详细讲解了C++中的默认成员函数,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载和取地址运算符重载等内容。重点分析了各函数的特点、使用场景及相互关系,如构造函数的主要任务是初始化对象,而非创建空间;析构函数用于清理资源;拷贝构造与赋值运算符的区别在于前者用于创建新对象,后者用于已存在的对象赋值。同时,文章还探讨了运算符重载的规则及其应用场景,并通过实例加深理解。最后强调,若类中存在资源管理,需显式定义拷贝构造和赋值运算符以避免浅拷贝问题。
|
1月前
|
存储 编译器 C++
类和对象(上)(C++)
本篇内容主要讲解了C++中类的相关知识,包括类的定义、实例化及this指针的作用。详细说明了类的定义格式、成员函数默认为inline、访问限定符(public、protected、private)的使用规则,以及class与struct的区别。同时分析了类实例化的概念,对象大小的计算规则和内存对齐原则。最后介绍了this指针的工作机制,解释了成员函数如何通过隐含的this指针区分不同对象的数据。这些知识点帮助我们更好地理解C++中类的封装性和对象的实现原理。
|
2月前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
1月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
109 6
|
2月前
|
算法 编译器 C++
模拟实现c++中的vector模版
模拟实现c++中的vector模版
|
1月前
|
编译器 C++
类和对象(下)C++
本内容主要讲解C++中的初始化列表、类型转换、静态成员、友元、内部类、匿名对象及对象拷贝时的编译器优化。初始化列表用于成员变量定义初始化,尤其对引用、const及无默认构造函数的类类型变量至关重要。类型转换中,`explicit`可禁用隐式转换。静态成员属类而非对象,受访问限定符约束。内部类是独立类,可增强封装性。匿名对象生命周期短,常用于临时场景。编译器会优化对象拷贝以提高效率。最后,鼓励大家通过重复练习提升技能!
|
3月前
|
C++ 芯片
【C++面向对象——类与对象】Computer类(头歌实践教学平台习题)【合集】
声明一个简单的Computer类,含有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,以及两个公有成员函数run、stop。只能在类的内部访问。这是一种数据隐藏的机制,用于保护类的数据不被外部随意修改。根据提示,在右侧编辑器补充代码,平台会对你编写的代码进行测试。成员可以在派生类(继承该类的子类)中访问。成员,在类的外部不能直接访问。可以在类的外部直接访问。为了完成本关任务,你需要掌握。
116 19