c++语言编程题两题,希望高手帮解答一下,谢谢!

第一题:
定义一个字符串类String,包含数据成员int size和char *str,成员函数:带一个char*类型参数构造函数、流插入运算字符“<<”重载函数使之能实现字符串的输出。编写主函数构造一个字符串并输出。
第二题:
定义一个坐标点类point,包含数据成员x和y,成员函数:无参构造函数、带两个参数构造函数、“+”运算符重载函数使之能实现两个坐标点的加法运算。编写主函数求两个坐标点之和。

第一题:

#include<iostream>
#include<cstring>

using namespace std;

class String
{
      
friend ostream& operator<<(ostream& output, String &a); 
friend int main();

private:
    int size;
    char *str;                 
public:
       String(char * a);
};

String::String(char * a)
{
           str = a;
           size = strlen(a);
}

ostream& operator<<(ostream& output, String &a)
{
           int i;
           int n=a.size;
           for(i=0;i<n;i++){
               output<<a.str[i];    
           }
           return output; 
}

int main()
{
    char *a = "Hello world!";
    cout << a << endl;
    String s = String(a);
    cout << s;
    cin.get();
    return 0;
}

第二题:

#include<iostream>

using namespace std;

class point
{
      
friend point operator+(const point& a, const point& b);
friend ostream& operator<<(ostream& output, point& a); 
friend int main();

private:
    double x;
    double y;

public:
    point()
    {
        x=0;
        y=0;
    }
    point(double xx, double yy){
        x=xx;
        y=yy;
    }
   
};

point operator+(const point& a, const point& b)
{
    point c;
    c.x= a.x+b.x;
    c.y= a.x+b.y;
    return c;
}

ostream& operator<<(ostream& output, point& a)
{
    output << a.x << " " << a.y;
    return output;
}
int main()
{
    point a = point();
    cout << a << endl;
    
    point b = point(-1,-1);
    cout << b << endl;
    
    point c = a+b;
    cout << c << endl;
    
    cin.get();
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-02-26
入门就是要初步对编程的思想有个了解,并且能编程解决一些小问题。入门一定要选好书,难度如果太低了会造成对读者的误导,以为编程就是这么回事;也不能太高深,免的打击读者的信心。既然编程的入门要求是编一些小程序解决小问题,那么就没必要直接学习C++,我觉得循环、判断、跳转、指针才是最基本的编程思想,学通了这些对以后学习C++很有帮助。 我建议应该先把C扎实地学好,不光要掌握C的语法,因为学习编程的最终目的是解决实际问题,所以还要适当掌握把实际问题与编程联系在一起的能力,我推荐《 C程序设计语言(第2版) 》,这本书是C语言之父写的,不仅详尽介绍了C的语法,还提供了很多实际的小问题作为实例,可以说看了这本书之后能为以后的编程学习打下一个扎实的基础! 提高: 学了《 C程序设计语言(第2版) 》后可以开始学习C++,一开始没必要钻很深的大部头书,《 Essential C++中文版 》非常适合初学,一个礼拜就可以看完,看了它可以快速对C++的全貌有个了解,虽然不会马上熟练掌握C++。 我们不能满足于解决“百钱买百鸡”,真正实现程序还有很多非功能上的问题,比如怎样做才能使用更少的内存?怎样做才能更快?在熟悉一门语言后,一定要认真学习的就是数据结构!数据结构保证了我们的程序能以高效的方式运行。《 数据结构算法与应用C++语言描述 》是本好书,它提供了很多实际的例子,比如火车站调度、走迷宫,相比于“百钱买百鸡”这种问题又提高了一个层次。 接下来专心提高C++的水平,可以选择《 C++编程思想 》,然后再看《 Effective C++中文版 2nd Edition 》和《 More Effective C++中文版 》,这两本书介绍了高效使用C++的宝贵经验,语言很幽默。至此,你已经可以自称是C++程序员了,恭喜! 下面介绍几本书,有兴趣的朋友可以选读。如果想高效地使用C++开发软件,STL一定要学!《 C++标准程序库—自修教程与参考手册 》可以帮你学会STL的使用,而且开发时可以拿它做手册,有了前面数据结构的基础,学习STL的使用不会遇到什么困难。如果你还想知道STL的设计思想及原理,不妨看看《 泛型编程与STL 》。有些朋友可能会问,C++的面向对象机制是怎么实现的?《 深度探索C++对象模型 》给了你答案,它剖析了C++底层的实现。最后,如果想有一本C++大百科全书,那非《 C++程序设计语言(特别版) 》莫属!这本书是C++之父Bjarne Stroustrup的大作,算是书中权威。 再提高: 到现在为止,你已经熟练掌握C++语言了,现在需要提高的就是设计思想,面向对象(OO)的最大特点是把我们的注意力从系统的执行体系结构转移到概念体系结构,因此利用OO开发大型系统会更轻松。但是经常可以看到人们拿着C++用面向过程的思想解决问题,缺乏的就是面向对象的设计思想,也难怪,在这里之前看的书都没有教你怎样用面向对象的方法开发系统,顶多介绍了面向对象是什么,看了这么多书难免会先入为主,《C语言程序设计》(或者你看的第一本编程书)中的思路潜在的影响着我们。这条鸿沟就由《 设计模式--可复用面向对象软件的基础 》来补救吧!所谓模式就是前人总结出来的、经过千锤百炼的一种系统结构,设计模式展示了23个经典的模式,通过学习、理解,你可以看到什么是真正的面向对象。 看了《 设计模式--可复用面向对象软件的基础 》后,有时间不妨看看《 重构--改善既有代码的设计(中文版) 》,学了面向对象的设计思想后看它,可以站在一定高度重新审视一下自己的C++编程水平。 结束语: 在学习编程的过程中一定要注意多实践!学习时到CSDN论坛同大家讨论是个不错的办法,那里的高手如过江之鲫,你会找到许多热心的朋友与您一起学习,能通过学习编程交些朋友不是更好吗?最后,希望您能成为优秀的程序设计师,为中国软件的发展贡献一份力量!
第2个回答  2023-04-03
class CVehicle
{
private:
char* p_id;
long total_person;
double total_weight;

public:
CVehicle(char* id, long person, double weight)
{
p_id = strdup(id);
totol_person = person;
weight = totoal_weight;
}
~CVehicle(void)
{
if(p_id)
free(p_id);
}
void set_id(char* id)
{
if(p_id)
free(p_id);
p_id = strdup(id);
}
char* getVehicleID(void)
{
return p_id;
}
long getTotalPerson(void)
{
return total_person;
}
long getTotalWeight(void)
{
return total_weight;
}
bool operator==(const CVehicle& another)
{
return( strcmp(p_id, another.p_id) == 0 );
}
bool operator!=(const CVehicle& another)
{
return( strcmp(p_id, another.p_id) != 0 );
}
};

class CCar : public CVehicle
{
pirvate:
long carried_person;

public:
CCar(void)
: CVehicle("car", 8, 500.0)
{
carried_person = 0;
}
};

class CTruck : public CVehicle
{
pirvate:
double carried_weight;

public:
CTruck(void)
: CVehicle("truck", 4, 2000.0)
{
carried_weight = 0.0;
}
};
第3个回答  2015-02-26
#include <iostream>
#include <cstring>
using namespace std;

class String {
int size;
char *str;
public:
String(char* s) {
size = strlen(s);
str = new char[size+1];
strcpy(str, s);
}
friend ostream& operator<<(ostream& out, const String& s) {
out << s.str;
return out;
}
~String() {
delete[] str;
}
};

int main() {
char s[] = "Hello World!";
String str(s);
cout << str << endl;
}

 

#include <iostream>
using namespace std;

class point {
double x, y;
public:
point() : x(0), y(0) {}
point(double _x, double _y) : x(_x), y(_y) {}
point operator+(const point& another) {
return point(x + another.x, y + another.y);
}
friend ostream& operator<<(ostream& out, const point& p) {
out << "(" << p.x << ", " << p.y << ")";
return out;
}
};

int main() {
point a(3,4), b(4,5);
point c = a + b;
cout << "c = " << c << endl;
}