其他分享
首页 > 其他分享> > 第七次课——10.10

第七次课——10.10

作者:互联网

作业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