C++程序设计-有参函数解析

需积分: 32 3 下载量 56 浏览量 更新于2024-08-19 收藏 8.81MB PPT 举报
"有参函数是C++编程中一个重要的概念,它允许主调函数向被调函数传递数据,同时被调函数也能将结果返回给主调函数。这种数据传递方式是程序设计中实现功能模块化和代码复用的关键。在C++中,函数的声明通常包括类型说明、函数名和形式参数列表。形式参数列表是在函数调用时用来传递实际参数的位置,它们在函数定义中只起到占位符的作用。 C++的发展历程是这样的:从最初的BCPL和B语言,经过Dennis Ritchie和Brian Kernighan的改进,诞生了C语言。C语言因其结构化特性、高效执行和良好的可移植性而广受欢迎。C++是在C语言的基础上添加了面向对象的特性,如类、对象、继承、多态等,使得程序设计更加灵活和强大。 C++语言的特点包括: 1. 结构化编程:C++支持结构化编程,允许程序员将复杂的问题分解成独立的函数,提高了代码的可读性和维护性。 2. 高级语言和汇编语言特性结合:C++拥有丰富的运算符,包括算术逻辑运算和位运算,既能进行复杂的逻辑控制,又能直接操作硬件。 3. 可移植性:C++编写的程序可以在不同的计算机平台之间轻松移植,无需或只需少量修改。 4. 自由度高的语法:C++的语法较为宽松,为熟练的程序员提供了设计高质量、通用程序的灵活性,但也增加了初学者的学习难度。 在学习有参函数时,需要注意以下几点: - 参数类型:函数的形式参数和实际参数需要匹配,类型必须一致。 - 参数传递:C++支持值传递和引用传递。值传递会复制参数值,而引用传递则是传址,直接操作原始变量。 - 返回值:函数可以通过return语句返回结果,返回值类型需要在函数声明时指定。 - 函数重载:C++允许同一函数名但参数列表不同的函数存在,这是实现多态性的一种方式。 调试C++程序时,虽然相对困难,但理解语法规则、利用调试工具可以帮助找出程序错误。熟练掌握有参函数的概念和用法对于进行C++编程至关重要,尤其在游戏开发等需要高效计算和模块化设计的领域。"

#include<iostream> using namespace std; class Book { private: string bookname; string authtors; string publishing_house; int pages; double price; public: Book() { bookname = ""; authtors = ""; publishing_house = ""; pages = 0; price = 0; } Book(string bk, string auth, string pub, int pag,double pri) { bookname = bk; authtors = auth; publishing_house = pub; pages = pag; price = pri; } ~Book(){} string getBookname() const { return bookname; } void setBookname(char*name){ bookname=name; } string getAuthtors() const { return authtors; } void setAuthtors(char* auth) { authtors = auth; } string getPublishing_house() const { return publishing_house; } void setPublishing_house(char* pub) { publishing_house = pub; } int getPages() const { return pages; } void setPages(int pag) { pages = pag; } double getPrice() const { return price; } void setPrice(double pri) { price = pri; } }; int main() { Book book1("《C++程序设计》", "谭浩强编著", 485, "清华大学出版社", 36.00); Book book2("《吹牛大王历险记》", "拉斯伯等编著", 149, "天津人民出版社", 12.80); cout << "书名:" << book1.getBookname() << endl; cout << "作者:" << book1.getAuthtors() << endl; cout << "页数:" << book1.getPages() <<"页"<< endl; cout << "出版社:" << book1.getPublishing_house() << endl; cout << "价格:" << book1.getPrice() <<"元"<< endl; cout << endl; cout << "书名:" << book2.getBookname() << endl; cout << "作者:" << book2.getAuthtors() << endl; cout << "页数:" << book2.getPages() << "页" << endl; cout << "出版社:" << book2.getPublishing_house() << endl; cout << "价格:" << book2.getPrice() << "元" << endl; return 0; }这个代码如何修改

2023-05-19 上传

优化以下代码:#include <iostream> #include<string.h> using namespace std; class Book { private:     char bookname[30];     char authers[30];     char publishing_house[40];     int pages;     float price; public:     char getbookname();     char getauthers();     char getpublishing_house();     int getpages();     float getprice();          void setbookname(char *a);     void setauthers(char *a);     void setpublishing_house(char *a);     void setpages(int a);     void setprice(float a);     Book(char*a,char*b,cahr*c,int d,float e)     {         strcpy(bookname,a);         strcpy(authers,b);         strcpy(publishing_house,c);         pages=d;         price=e;     } }; char getbookname() {     char*a=bookname;     return a; } char getauthers() {     char*a=authers;     return a; } char getpublishing_house() {     char*a=publishing_house;     return a; } int getpages() {     int a=pages;     return a; } float getprice() {     float a=price;     return a; } void setbookname(char *a) {     strcpy(bookname,a); } void setauthers(char *a) {     strcpy(authers,a); } void setpublishing_house(char *a) {     strcpy(publishing_house,a); } void setpages(int a) {     pages=a; } void setprice(float a) {     price=a; } int main() {      Book a(char a="《C++程序设计》",char b="谭浩强编制",char c="清华大学出版社",d=484,e=36.00);      Book b(char a="《吹牛大王历险记》",char b="拉斯伯等编著",char c="天津人民出版社",d=149,e=12.80);     cout<<a.getbookname()<<"  "<<a.getauthers()<<"  "<<a.getpublishing_house()<<"  "<<"页数:"<<a.getpages()<<"价钱:"<<a.getprice()<<endl;     cout<<b.getbookname()<<"  "<<b.getauthers()<<"  "<<b.getpublishing_house()<<"  "<<"页数:"<<b.getpages()<<"价钱:"<<b.getprice()<<endl;     return 0; }

2023-05-22 上传