ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

第七次课——10.10

2021-10-27 19:35:49  阅读:167  来源: 互联网

标签:return cout int 第七次 c2 10.10 c1 myComplex


作业26

#include <iostream>

#include <stdlib.h>

#include <string>

using namespace std;

class student

{

  private:

    string name1;

    int age;

  public:

    void setStudent(string, int);

    void setName(string);

    string getName();

    void setAge(int);

    int getAge();

    void printStudent() const;

};

//在类体外定义成员函数

void student::setStudent(string s, int a)

{

  name1 = s;

  age = a;

}

void student::setName(string n)

{

  name1 = n;

}

string student::getName()

{

  return name1;

}

void student::setAge(int aa)

{

  age = aa;

}

int student::getAge()

{

  return age;

}

void student::printStudent() const

{

  cout << "姓名:" << name1 << endl;

  cout << "年龄:" << age << endl;

  cout << endl;

}

int main()

{

  student s;

  s.setStudent("zhangsan", 21);

  s.getName();

  s.getAge();

  s.printStudent();

  system("pause");

  return 1;

}

作业27

#include <iostream>

using namespace std;

class CType

{

  private:

    int radius;

    int width;

  public:

    CType() :radius(16), width(185)//参数列表初始化私有变量

    {

    }

    CType(int r, int w) :radius(r), width(w)

    {

    }

    int getRadius()

    {

      return radius;

    }

    int getWidth()

    {

      return width;

    }

};

class CCar

{

  private:

    int price;

    CType type;

  public:

    CCar();

    CCar(int p, int tr, int tw);

    int getPrice()

    {

      return price;

    }

    CType getCType()

    {

      return type;

    }

};

CCar::CCar()

{

  price = 156000;

  CType();

}

CCar::CCar(int p, int tr, int tw) :price(p), type(tr, tw){};

int main()

{

  CCar car(128000, 17, 225);

  cout << "price=" << car.getPrice();

  cout << "\tCType.Radius=" << car.getCType().getRadius();

  cout << "\tCType.Width=" << car.getCType().getWidth() << endl;

  CCar car1;

  cout << "price=" << car1.getPrice();

  cout << "\tCType.Radius=" << car1.getCType().getRadius();

  cout << "\tCType.Width=" << car1.getCType().getWidth() << endl;

  system("pause");

  return 1;

}

作业28

#include <iostream>

using namespace std;

class myComplex

{

  private:

    double real, imag;

  public:

    myComplex();

    myComplex(double r, double i);

    friend myComplex addCom(myComplex c1, myComplex c2);f

    riend void outCom(myComplex c);

};

myComplex::myComplex()

{

  real = 0;

  imag = 0;

}

myComplex::myComplex(double r, double i)

{

  real = r;

  imag = i;

}

myComplex addCom(myComplex c1, myComplex c2)

{

  return myComplex(c1.real + c2.real, c1.imag + c2.imag);

}

void outCom(myComplex c)

{

  cout << "(" << c.real << "," << c.imag << ")";

}

int main(

{

  myComplex c1(1, 2), c2(3, 4), res;

  res = addCom(c1, c2);

  outCom(c1);

  cout << "+";

  outCom(c2);

  cout << "=";

  outCom(res);

  cout << endl;

  system("pause");

  return 0;

}

作业29

#include <iostream>

using namespace std;

class Location

{

  public:

    int x, y;

    void init(int initx, int inity);

    int Getx();

    int Gety();

};

void Location::init(int initx, int inity)

{

  x = initx;

  y = inity;

}

int Location::Getx()

{

  return x;

}

int Location::Gety()

{

  return y;

}

void display(Location &rL)

{

  cout << rL.Getx() << " " << rL.Gety() << endl;

}

int main()

{

  Location A[5] = { { 5, 5 }, { 3, 3 }, { 1, 1 }, { 2, 2 }, { 4, 4 } };

  Location *rA = A;

  A[3].init(7, 3);

  rA->init(7, 8);

  for (int k = 0; k < 5; k++)

    display(*(rA++));

  system("pause");

  return 1;

}

作业30

#include <iostream>

using namespace std;

class myComplex

{

  private:

    double real, imag;

  public:

    myComplex();

    myComplex(double r, double i);

    void outCom();

    myComplex operator-(const myComplex &c);

    friend myComplex operator+(const myComplex &c1, const myComplex &c2);

};

myComplex::myComplex()

{

  real = 0;

  imag = 0;

}

myComplex::myComplex(double r, double i)

{

  real = r;

  imag = i;

}

myComplex operator+(const myComplex &c1, const myComplex &c2)

{

  return myComplex(c1.real + c2.real, c1.imag + c2.imag);

}

myComplex myComplex::operator-(const myComplex &c)

{

  return myComplex(this->real - c.real, this->imag - c.imag);

}

void myComplex::outCom()

{

  cout << "(" << real << "," << imag << ")";

}

int main()

{

  myComplex c1(1, 2), c2(3, 4), res;

  c1.outCom();

  cout << "operator+ ";

  c2.outCom();

  cout << "=";

  res = c1 + c2; //重载的是“+”

  res.outCom();

  cout << endl;

  c1.outCom();

  cout << "operator-";

  c2.outCom();

  cout << "=";

  res = c1 - c2;

  res.outCom();

  system("pause");

  return 1;

}

标签:return,cout,int,第七次,c2,10.10,c1,myComplex
来源: https://www.cnblogs.com/cnxm/p/15472523.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有