首页 > 分享 > 面向对象编程练习

面向对象编程练习

1、编写一个程序,已有若干个学生数据,包括学号、姓名、成绩,要求输出这些学生数据并计算平均分。

#include<bits/stdc++.h>

using namespace std;

class Student{

protected:

string num;

string name;

int score;

static int number;

static int ans_score;

public:

Student(string num,string name,int score):num(num),name(name),score(score){

number++;

ans_score+=this->score;

}

void getInfo();

static void get_avg();

};

int Student::number=0;

int Student::ans_score=0;

void Student::getInfo()

{

cout<<left<<setw(10)<<num<<setw(10)<<name<<setw(10)<<score<<endl;

}

void Student::get_avg(){

cout<<left<<setw(10)<<ans_score/number<<endl;

}

int main()

{

Student fanbo("1","fanbo",100);

Student zeng("2","zeng",100);

Student test("3","test",0);

cout<<left<<setw(10)<<"学号"<<setw(10)<<"姓名"<<setw(10)<<"分数"<<endl;

fanbo.getInfo();

zeng.getInfo();

test.getInfo();

test.get_avg();

}

cpp

运行

2题目:图书管理系统 任务描述

设计一个简单的图书管理系统,要求实现以下功能:

图书类(Book):

属性:书名(字符串),作者(字符串),ISBN号(字符串),价格(浮点型)。方法:构造函数、析构函数、显示图书信息的成员函数showInfo()。

图书管理类(Library):

属性:存储图书的容器(可以使用std::vector<Book>)。方法:添加图书的成员函数addBook(const Book&)。删除图书的成员函数removeBook(const string& isbn)。显示所有图书信息的成员函数showAllBooks()。根据ISBN搜索图书的成员函数findBookByISBN(const string& isbn)。要求 实现上述所有类和方法。演示创建几本不同的图书,并添加到图书管理系统中。演示搜索特定ISBN的图书并显示其信息。演示删除一本图书,然后显示当前系统中的所有图书信息。

这个题目将考查你对于面向对象编程的理解,包括类的设计、对象的创建和管理、以及容器的基本使用。通过这个练习,你将加深对C++标准库容器以及对象操作的理解。

代码

注意访问不是自己的类的变量时通过其成员函数去访问 例如 get等

#include<bits/stdc++.h>

using namespace std;

class Book{

protected:

string book_name;

string writer;

string ISBN;

double price;

public:

Book(string bn,string wr,string is,double pr):

book_name(bn),writer(wr),ISBN(is),price(pr){}

~Book(){}

void showInfo()

{

cout<<book_name<<" "<<writer<<" "<<ISBN<<price<<endl;

}

string getInfoISBN(){

return ISBN;

}

};

class Library{

protected:

vector<Book> bk;

public:

void addBook(Book book)

{

bk.push_back(book);

}

void deletebook(string isbn)

{

for(int i=0;i<bk.size();i++){

if(bk[i].getInfoISBN()==isbn){

bk.erase(i+bk.begin());

}

}

}

void showAllBooks()

{

for(int i=0;i<bk.size();i++)

{

bk[i].showInfo();

}

}

void changebook(Book book)

{

for(int i=0;i<bk.size();i++){

if(bk[i].getInfoISBN()==book.getInfoISBN()){

bk[i]=book;

}

}

}

};

int main()

{

Book book1("Book1", "Author1", "ISBN1", 100);

Book book2("Book2", "Author2", "ISBN2", 150);

Book book3("Book222222222", "Author222222", "ISBN2", 150);

Library fanbo;

fanbo.addBook(book1);

fanbo.addBook(book2);

fanbo.changebook(book3);

fanbo.showAllBooks();

}

cpp

运行

3题目:宠物寄养中心管理系统

任务描述:

设计一个宠物寄养中心管理系统,利用面向对象编程的特性(封装、继承、多态等),实现以下功能:

宠物类(Pet):

属性:宠物名字(字符串)、年龄(整型)、类型(字符串)方法:构造函数、析构函数、显示宠物信息的成员函数 showInfo(),该函数应当是虚函数以支持多态。

狗类(Dog)和猫类(Cat):

继承自宠物类(Pet)狗类额外属性:品种(字符串)猫类额外属性:颜色(字符串)各自实现 showInfo() 函数,展示特定信息。

寄养中心类(PetCareCenter):

属性:存储宠物的容器(建议使用 std::vector<Pet*>)方法:增加宠物:接受一个宠物指针,将宠物添加到容器中删除宠物:按宠物名字删除宠物显示所有宠物信息:遍历容器,调用每个宠物的 showInfo() 方法

扩展功能(如果上述内容实现顺利,可以尝试以下扩展功能):

实现一个简单的宠物寄养费用计算器,费用根据宠物类型及特定属性(如:狗的品种、猫的颜色)来计算。为宠物类添加一个虚函数 calculateFee(),并在狗类和猫类中重写该函数。

要求:

演示创建至少两个Dog对象和两个Cat对象,并将它们添加到寄养中心。演示使用寄养中心的功能,包括增加宠物、删除宠物、显示所有宠物信息。如果实现了扩展功能,演示计算寄养费用的功能

#include<bits/stdc++.h>

using namespace std;

class Pet{

public:

string name;

int age;

string classfy;

public:

Pet(string name,int age,string classfy):

name(name),age(age),classfy(classfy){}

~Pet(){}

virtual void showInfo()

{

cout<<name<<" "<<age<<" "<<classfy<<endl;

}

};

class Dog:public Pet

{

public:

string pingzhong;

public:

Dog(string name,int age,string classfy,string pin):

Pet(name,age,classfy),pingzhong(pin){}

void showInfo()

{

cout<<name<<" "<<age<<" "<<classfy<<" "<<pingzhong<<endl;

}

};

class Cat :public Pet

{

public:

string color;

Cat(string name,int age,string classfy,string color):

Pet(name,age,classfy),color(color){}

void showInfo()

{

cout<<name<<" "<<" "<<classfy<<" "<<color<<endl;

}

};

class PetCareCenter{

public:

vector<Pet*> pet;

public:

void addpet(Pet * pt)

{

pet.push_back(pt);

}

void deletee(string name)

{

for(auto it=pet.begin();it!=pet.end();it++)

{

if((*it)->name==name)

{

pet.erase(it);

}

}

}

void show_all_Info()

{

for(auto it=pet.begin();it!=pet.end();it++)

{

(*it)->showInfo();

}

}

};

int main()

{

PetCareCenter center;

Dog dog1("fanbo",2,"狗","亚洲");

Dog dog2("zeng",4,"狗","非洲");

Cat cat1("ben",3,"猫","白色");

Cat cat2("hui",5,"猫","黑色");

center.addpet(&dog1);

center.addpet(&dog2);

center.addpet(&cat1);

center.addpet(&cat2);

center.show_all_Info();

center.deletee("fanbo");

center.show_all_Info();

}

cpp

运行

4字符串+重载+加法器

#include<string>

#include<string.h>

#include<iostream>

using namespace std;

char * reversee(char * sh)

{

char * tmp=sh;

int st=0,ed=strlen(tmp);

for(int i=st,j=ed-1;i<=j;i++,j--)

swap(tmp[i],tmp[j]);

return tmp;

}

class LongLongInt{

public:

char * ch;

public:

LongLongInt(){};

LongLongInt( string str)

{

char * sh= new char[sizeof(str)+1];

strcpy(sh,str.c_str());

sh[sizeof(str)]='';

ch=reversee(sh);

}

LongLongInt(LongLongInt & fanbo);

bool operator >=(LongLongInt & fanbo)

{

char * tmp1=reversee(ch);

char * tmp2=reversee(fanbo.ch);

if(strcmp(tmp1,tmp2)>=0)

return true;

}

void getInfo();

void add_sum(LongLongInt & a,LongLongInt & b);

};

LongLongInt::LongLongInt( LongLongInt &fanbo)

{

ch=fanbo.ch;

}

void LongLongInt::add_sum(LongLongInt &a,LongLongInt & b)

{

int ans=0;

if(strlen(a.ch)<strlen(b.ch))

add_sum(b,a);

this->ch = new char[strlen(a.ch)+1];

int i=0;

for(;i<strlen(b.ch);i++)

{

int a1=a.ch[i]-'0';

int b1=b.ch[i]-'0';

this->ch[i]=(a1+b1+ans)%10+'0';

ans=(a1+b1+ans)/10;

}

while(i<strlen(a.ch))

{

int a1=a.ch[i]-'0';

this->ch[i]=(a1+ans)%10+'0';

ans=(a1+ans)/10;

i++;

}

return;

}

void LongLongInt::getInfo()

{

cout<<(ch)<<endl;

}

int main()

{

LongLongInt fanbo("510");

LongLongInt zeng("50");

LongLongInt ben(zeng);

LongLongInt son;

son.add_sum(fanbo,zeng);

son.getInfo();

}

cpp

运行

5.时钟 输出下一天

#include <iostream>

using namespace std;

int month_day[2][20]={

{0,31,29,31,30,31,30,31,31,30,31,30,31},

{0,31,28,31,30,31,30,31,31,30,31,30,31}

};

class Data

{

protected:

int year;

int month;

int day;

public:

Data(int y,int m,int d):year(y),month(m),day(d){}

bool judge(int year);

void show();

void get_nextday();

};

bool Data::judge(int year)

{

if(year%400==0||(year%4==0&&year%100!=0))

return 0;

return 1;

}

void Data::show()

{

cout<<"当前时间是"<<year<<"-"<<month<<"-"<<day<<endl;

}

void Data::get_nextday()

{

int tmp_day=day;

int tmp_month=month;

int tmp_year=year;

tmp_day++;

if(tmp_day>month_day[judge(this->year)][month])

{

tmp_day=1;

tmp_month++;

}

if(tmp_month>12)

{

tmp_month=1;

tmp_year+=1;

}

cout<<"明天时间是"<<tmp_year<<"-"<<tmp_month<<"-"<<tmp_day<<endl;

}

int main()

{

Data today(2018,12,31);

today.show();

today.get_nextday();

}

cpp

运行

相关知识

面向对象编程练习
JavaSE之面向对象编程
面向对象编程——以猫狗宠物商店出发
Python基本思想——面向对象编程
14天Java基础学习——第6天:面向对象编程(类与对象)
Java面向对象
面向对象编程题汇总
面向对象编程:宠物与犬类的Python实现详解
面向对象练习(3)
PHP面向对象(第一部分)

网址: 面向对象编程练习 https://m.mcbbbk.com/newsview1229051.html

所属分类:萌宠日常
上一篇: dnf宠物怎么丢弃 dnf宠物丢
下一篇: 常熟路长乐路附近周边哪里有宠物医