Experiment Class II and Objects


Class: B145A7 Student ID: 11 Grade:    

one. Purpose

1 . Understand the basic ideas of object-oriented programming;

2 . Master the concept, definition and usage of classes and objects.

3 . Master the access methods of members of different attribute objects.

two. Equipment and instruments used

Computer +Windows XP+Visual C++6.0

three. Experiment content and requirements

1. Define a class Cuboid that represents a cuboid. The data members include length (length), width (width), height (height) , and member functions include the input, output, calculation volume and surface area of ​​the cuboid. In the main function, define 3 cuboid objects and call member functions to complete their functions.

2. Define a student class Student , the data members include student number, name, math score, English score and C++ score, member functions include: input student information function; output student information function; set student information function; calculate student's information function A function of grade point average. Call the above functions in the main function to implement the corresponding functions.

3. Define a book class Book , which includes the following data members and member functions:

Data members: id (book number), bookname (book name), price (price), total (total number of books in stock), number (current number of remaining books)

Member function:

Input() - book information input;

Output() - book information output;

Borrow() - borrow books and display the current number of remaining books;

Restore() - Returns the book and displays the current number of remaining books.

In the main function, it is required to create some kind of book object, and perform simple input, output, borrowing and returning management of the book.

multiple choice:

4. Write the class according to the following requirements.

1 ) Define a date class Date , data members include year, month, day, member functions include:

Input() - date information input;

Output() - date information output;

Set() - set date information

2 ) Add a birth date member to the Student class in question 2 , and use the Date class to define it. Then modify the corresponding member function and add a member function GetAge to calculate and return the age of the student.

Define the object in the main function to test the above functions.

Four. Experimental procedure

Experiment one program code:

#include<iostream>

using namespace std;

class Cuboid

{

private:

float len,wit,high;

public:

void Input();

void Output();

void Volume();

void area();

};

void Cuboid::Input()

{

cout<<" Please enter the length of the cuboid: ";

cin >> len;

cout<<" Please enter the width of the cuboid: ";

cin>>wit;

cout<<" Please enter the height of the cuboid: ";

cin>>high; 

}

void Cuboid::Output()

{

cout<<"The length of the cuboid is: "<<len<<endl;

cout<<"The width of the cuboid is: "<<wit<<endl;

cout<<"The height of the cuboid is: "<<high<<endl; 

}

void Cuboid::Volume()

{

cout<<"The volume of the cuboid is: "<<len*wit*high<<endl;

}

void Cuboid::area()

{

cout<<"The surface area of ​​the cuboid is: "<<len*wit*2+len*high*2+wit*high*2<<endl;

}

intmain()

{

Cuboid t1,t2,t3;

   t1.Input();

t1.Output();

t1.Volume();

t1.area();

t2.Input();

t2.Output();

t2.Volume();

t2.area();

t3.Input();

t3.Output();

t3.Volume();

t3.area();

return 0;

}operation result:

 

 

Experiment 2 program code:

#include<iostream>

#include<string>

using namespace std;

 

class Student

{

private:

int num;

string name;

double english,math,c;

public:

void Input();

void Show();

int Are();

    void Set(int x,string y,double z,double h,double k);

};

void Student::Input()

{

cout<<" Please enter your student ID :";

cin>>num;

cout<<" Please enter your name :";

cin>>name;

cout<<" Please enter your math, English , c++ grades :"<<endl;

cin>>math>>english>>c;

}

void Student::Show()

{

cout<<"学号:"<<num<<endl;

cout<<"名字:"<<name<<endl;

cout<<"数学:"<<math<<endl;

cout<<"英语:"<<english<<endl;

cout<<"c++:"<<c<<endl;

}

int Student::Are()

{

return (math+english+c)/3;

}

void Student::Set(int x,string y,double z,double h,double k)

{

num=x;

name=y;

english=z;

math=h;

c=k;

 

}

intmain()

{

Student s;

s.Input();

s.Show();

cout<<"The system default is: "<<endl;

s.Set(2014,"yule",100,100,100);

s.Show();

cout<<"The average grade is :"<<s.Are()<<endl;

return 0;

}

operation result:

 

 

Experiment three program code:

#include<iostream>

#include<string>

#include<stdlib.h>

using namespace std;

class Book

{

private:

int id;

string name;

float price;

int total;

int num;

public:

void Input();

void Output();

void Borrow();

void Restore();

};

void Book::Input()

{

cout<<" Please enter book information: "<<endl;

cout<<"图书id";

cin>>id;

cout<<" Book name: "; 

cin>>name;

cout<<" Book price: ";

 cin>>price;

 cout<<" Total amount of books: ";

 cin>>total;

 cout<<" Book remaining: ";

 cin>>num; 

}

void Book::Output()

{

cout<<"图书id为:"<<id<<endl;

cout<<" Book name is: "<<name<<endl;

 cout<<" Book price: "<<price<<endl;

 cout<<" Total amount of books: "<<total<<endl;

 cout<<" Book remaining: "<<num<<endl;

}

void Book::Borrow()

{

int n;

cout<<" Please enter the borrowing amount: ";

cin>>n;

while(n>num||n<0)

{

cout<<"The input data is wrong, please re-enter "<<endl;

cin>>n;

}

num=num-n;

cout<<"The remaining amount of books is: "<<num<<endl;

}

void Book::Restore()

{

int m;

cout<<" Please enter the return amount: ";

cin>>m;

num=num+m;

cout<<"The remaining amount of books is: "<<num<<endl;

}

intmain()

{

Book s;

s.Input();

s.Output();

s.Borrow();

s.Restore();

return 0;

}

 

operation result:

 

Fourth question code

#include<iostream>

#include<iomanip>

using namespace std;

class Date

{

private:

int year,YEAR;

int month,MONTH;

int day,DAY;

public:

void Input();

void Output();

void Set(int YEAR,int MONTH,int DAY);

void Set_Intput();

};

void Date::Input()

{

cout<<" Please enter the year: ";

cin>>year;

cout<<" Please enter the month: ";

cin>>month;

while(month>12||month<0)

{

cout<<"The data is wrong, re-enter: ";

cin>>month; 

}

cout<<" Please enter the day: ";

cin>>day; 

while(day>31||day<0)

{

cout<<"The data is wrong, re-enter: ";

cin>>day;

}

}

void Date::Output()

{

cout<<setw(6)<<""<<setw(6)<<""<<setw(6)<<""<<endl;

cout<<setw(6)<<year<<setw(6)<<month<<setw(6)<<day<<endl; 

}

void Date::Set_Intput()

{

cout<<" Please enter reset information "<<endl;

cout<<" Please enter the year: ";

cin>>year;

cout<<" Please enter the month: ";

cin>>month;

while(month>12||month<0)

{

cout<<"The data is wrong, re-enter: ";

cin>>month; 

}

cout<<" Please enter the day: ";

cin>>day; 

while(day>31||day<0)

{

cout<<"The data is wrong, re-enter: ";

cin>>day;

}

}

void Date::Set(int YEAR,int MONTH,int DAY)

{

year=YEAR;

month=MONTH;

day=DAY;

}

intmain()

{

Date m;

m.Input();

m.Output();

m.Set_Intput();

 

m.Output();

return 0; 

}

 

Fives. Experimental summary

1. Problems encountered in the experiment and solutions

There is a big problem in understanding the setting function. Solution: Ask the teacher and classmates to solve this problem.

2. Personal gain and experience

Through this experiment, my proficiency in C++ has been further improved; I have also figured out a lot of problems that I don't understand.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325485144&siteId=291194637