5539,

Topic content :

P178 example 6.2

Define base class base, Contains private data members price and quantity. Define constructors and virtual functions to display the values of data members .

Define derived classes derived, public inheritance base. Contains data members life. Define constructor , Displays the data of the member and the virtual value of the function ..

A derived class object is defined in the main function mc, The initial value is 2000,2,18. Pointer to base class pc, Assign the address of the derived class object to pc. Implements the output of derived class data members .

Input / output description :
output : price is 2000 quantity is 2 life is 18

#include<iostream> using namespace std;
/* Define base class base, Contains private data members price and quantity. Define constructors and virtual functions to display the values of data members .
Define derived classes derived, public inheritance base. Contains data members life. Define constructor , And virtual functions to display the values of data members ..
A derived class object is defined in the main function mc, The initial value is 2000,2,18. Pointer to base class pc, Assign the address of the derived class object to pc. Implements the output of derived class data members . */ class
base{ private: int price; int quantity; public: base(int price,int quantity){
this->price=price; this->quantity=quantity; } virtual void output(){
cout<<"price is "<<price<<" quantity is "<<quantity<<endl; } }; class derived
:public base{ private : int life; public: derived(int price,int quantity,int
life):base(price,quantity){ this->life=life; } virtual void output(){
base::output(); cout<<"life is "<<life<<endl; } }; int main(){ derived
mc(2000,2,18); base *pc=&mc; pc->output(); }

5540,

Topic content :

P184 example 6.8

Define base class a1 and a2 A virtual function , Output a string “--a1--”, The other contains a non imaginary function , Output string “--a2--”.

Define derived classes b, Output string with function “--b--”.

Define the main function to define the object and pointer of two base classes respectively . An object that defines a derived class . Get the address of the base class object and assign it to the pointer of the base class , And call the function with the same name in the class . Get the address of the derived class object and assign it to the pointer of the base class , And call the function with the same name in the class . To achieve the required output .

Input / output description :
output : --a1-- --a2-- --b-- --a2--
 
#include<iostream> using namespace std;
/* Define base class a1 and a2 A virtual function , Output a string “--a1--”, The other contains a non imaginary function , Output string “--a2--”. Define derived classes b, Output string with function “--b--”.
Define the main function to define the object and pointer of two base classes respectively . An object that defines a derived class . Get the address of the base class object and assign it to the pointer of the base class , And call the function with the same name in the class . Get the address of the derived class object and assign it to the pointer of the base class ,
And call the function with the same name in the class . To achieve the required output */ class a1{ public: virtual void output(){
cout<<"--a1--"<<endl; } }; class a2{ public: void output(){
cout<<"--a2--"<<endl; } }; class b :public a1,public a2{ public: void output(){
cout<<"--b--"<<endl; } }; int main(){ a1 obj1; a2 obj2; b obj3; a1 *e1=&obj1;
e1->output(); a2 *e2=&obj2; e2->output(); e1=&obj3; e1->output(); e2=&obj3;
e2->output(); }

5541,

Topic content :

P199 example 7.4

Defines a class that contains two private data members , It has friends ++ Operator overloaded function , They are pre self addition and post self addition respectively . To achieve the required output .

Input / output description :
output : x: 11,y: 22 x: 12,y: 23 x: 13,y: 24 x: 14,y: 25 x: 15,y: 26
 
#include<iostream> using namespace std; /* Defines a class that contains two private data members ,
It has friends ++ Operator overloaded function , They are pre self addition and post self addition respectively . To achieve the required output . */ class A{ public: int x,y; public: A(int
x,int y){ this->x=x; this->y=y; } friend A operator++(A &obj); }; A
operator++(A &obj){ obj.x++; obj.y++; return obj; } int main(){ A obj(11,22);
for(int i=1;i<=5;i++){ cout<<"x: "<<obj.x<<","<<"y: "<<obj.y<<endl;
obj=operator++(obj); } }

5542,

Topic content :

P204 example 7.6

Overloading operators with member functions ++, Including pre self addition and post self addition to achieve the required output .

Input / output description :
output : x: 11,y: 22 x: 12,y: 23 x: 13,y: 24 x: 14,y: 25 x: 15,y: 26
 
#include<iostream> using namespace std; class A{ public: int x,y; public:
A(int x,int y){ this->x=x; this->y=y; } A operator++(){ this->x++; this->y++;
return *this; } }; int main(){ A obj(11,22); for(int i=1;i<=5;i++){ cout<<"x:
"<<obj.x<<","<<"y: "<<obj.y<<endl; obj.operator++(); } }

5543,

Topic content :

P216 example 7.13

Overloading operator functions as friend functions , And use overloaded operators “<<” Output date .

That is, the object appears in the output stream of the date class .

Input / output description :
output : 2016.4.28
 
#include<iostream> using namespace std; class Date{ protected: int
day,month,year; public: Date(int day,int month,int year){ this->day=day;
this->month=month; this->year=year; } friend ostream& operator<<(ostream
&os,const Date &d); }; ostream& operator<<(ostream &os,const Date &d){
cout<<d.year<<"."<<d.month<<"."<<d.day<<endl; return os; } int main(){ Date
d(28,4,2016); cout<<d; }

5533,

Topic content :

Programming , Calculate the running time of the car , First, establish the base class car, It contains data members distance Store the distance between two points . Assumed distance in miles , Speed per hour 80 mile , Using virtual functions travel_time() Calculate and display the time through this distance . In derived classes kilometre in , The assumed distance is calculated in kilometers , Speed per hour 120 Kilometer , Use function travel_time() Calculate and display the time through this distance . The distance is entered from the keyboard (80 mile ~128 kilometre )

Input / output description :
input : 10000 output : base time is 78.125 derived time is 83.3333
 
#include<iostream> using namespace std; class car { public: car (double
distance) { this->distance=distance; } virtual void travel_time() {
// virtual function , In miles per hour 80 Calculate travel time cout << "base time is " << distance / (80*1.6) << endl;
}; protected: // Of derived classes distance The constructor of the base class is called when the member is constructed double distance; }; class kilometer
:public car { public: virtual void travel_time() { // virtual function , In kilometers per hour 120 Calculate travel time
cout << "derived time is " << distance / 120 <<endl; } kilometer(double
distance):car(distance){ } }; int main() { double x; cin>>x; car c1(x);
// Create base class object c1 kilometer c2(x); // derived class object c2 car *index = &c1; // Defines a pointer to a base class object index, point c1
index->travel_time(); // The base class object was called c1 of travel_time function index = &c2; // Object pointing to a derived class c2
index->travel_time(); // A derived class object was called c2 of travel_time function return 0; }

5534,

Topic content :

Make a program , Overloaded operator member “+” and “-”, Add and subtract two 2D arrays , The value of the first two-dimensional array is required to be set by the constructor , The value of another two-dimensional array is entered by the keyboard .

Input / output description :
first 3*3 The two-dimensional array is set by the constructor , Value is :1 2 3 4 5 6 7 8 9 the other one 3*3 The value of the two-dimensional array is entered by the keyboard , The value entered from the keyboard is 1 2 3 4 5 6
7 8 9 Two objects are defined based on the class of two-dimensional array , Add and subtract , The result is assigned to another object . display output . input :1 2 3 4 5 6 7 8 9 output : 2 4 6
8 10 12 14 16 18 0 0 0 0 0 0 0 0 0
 
#include<iostream> using namespace std; const int n=3; class Array{ public:
Array(){ int k=1; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ p[i][j]=k++; } }
} Array(int a[n][n]){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){
p[i][j]=a[i][j]; } } } Array operator+(Array b){ for(int i=0;i<n;i++){ for(int
j=0;j<n;j++){ this->p[i][j]+=b.p[i][j]; } } return *this; } Array
operator-(Array b){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){
this->p[i][j]-=b.p[i][j]; } } return *this; } void show(){ for(int
i=0;i<n;i++){ for(int j=0;j<n;j++){ cout<<p[i][j]<<" "; } cout<<endl; } }
public: int p[n][n]; }; int main(){ Array a1; int b[n][n]; for(int
i=0;i<n;i++){ for(int j=0;j<n;j++){ cin>>b[i][j]; } } Array a2(b); Array a3=a1;
a1=a1+a2.p; a1.show(); a1=a3; a1=a1-a2.p; a1.show(); }

5535,

Topic content :

  Make a program , UFIDA function overload operator “+” and “-”, Add and subtract two 2D arrays , The value of the first two-dimensional array is required to be set by the constructor , The value of another two-dimensional array is entered by the keyboard .

Input / output description :
input :1 2 3 4 5 6 7 8 9 output : 2 4 6 8 10 12 14 16 18 0 0 0 0 0 0 0 0 0
 
#include<iostream> using namespace std; const int n=3; class Array{ public:
Array(){ int k=1; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ p[i][j]=k++; } }
} Array(int a[n][n]){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){
p[i][j]=a[i][j]; } } } void show(){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){
cout<<p[i][j]<<" "; } cout<<endl; } } friend Array operator+(Array a,Array b);
friend Array operator-(Array a,Array b); protected: int p[n][n]; }; Array
operator+(Array a,Array b){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){
a.p[i][j]+=b.p[i][j]; } } return a; } Array operator-(Array a,Array b){ for(int
i=0;i<n;i++){ for(int j=0;j<n;j++){ a.p[i][j]-=b.p[i][j]; } } return a; } int
main(){ Array a1; int b[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){
cin>>b[i][j]; } } Array a2(b); Array a3; a3=operator+(a1,a2); a3.show();
a3=operator-(a1,a2); a3.show(); }

Technology
©2019-2020 Toolsou All rights reserved,
evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possible Experiment 4 Automated test tools - software test mysql Export data sql sentence _mysql according to sql Query statement export data Create a thread ——— Javaweb (3) Data structure experiment ( three )—— Stacks and queues TS stay vue2 Writing in the project web Front end signature plug-in _signature_pad Plug in implements electronic signature function docker Where is the image stored Qt Getting Started tutorial 【 Basic controls 】QCalendarWidget calendar control springboot How to get reality in ip address