Home > Back-end >  Novice new C, encounter a problem, the constructor of a class of private inheritance and great spiri
Novice new C, encounter a problem, the constructor of a class of private inheritance and great spiri

Time:09-27

# # ifndef ZY2_H_
# define ZY2_H_
#include//use the string class
#include//using valarray
#include

Typedef STD: : valarray ArrayInt;

templateThe class Pair
{
Private:
T1 a;
T2 b;
Public:
Pair (const T1 & amp; Aval, const T2 & amp; (aval) bval) : a, b (bval) {}
Pair () {}
T1 & amp; The first () {return a; }//return type for the T1 & amp; That can modify the data, for general object
T2 & amp; The second () {return b; }//return type for the T2 & amp; That can modify the data, for general object
T1 first const () {return a; }//return type for T1, after add const, make can't modify the data for constant object
T2 second const () {return b; }//return type for T2, after add const, make can't modify the data for constant object
Pair & Operator=(const Pair & P);
};


Typedef Pair PairArray;


PairArray & amp; Pair : : operator=(const PairArray & amp; P)//
{
If (this==& amp; P)
return *this;
A=p.a.
B=p.b.
return *this;
}







The class Wine: private STD: : string, private Pair
{
Private:
Int years;
Public:
Wine () {}
Wine (const char * l, int y, const int yr [], const int bot [])
STD: : string (l), years (y), Pair (ArrayInt (yr, y), ArrayInt (bot, y)) {} //write PairArray (ArrayInt (yr, y), ArrayInt (bot, y)), the compiler can accept, but such writing PairArray (ArrayInt (yr, years), ArrayInt (bot, years)), the compiler can not accept. Great god teach genuflect is begged
Wine (const char * l, int y) : STD: : string (l), years (y), Pair () {}//yb () calls the constructor Pair ()
Void GetBottles ();
Const STD: : string & amp; Const Label () {return (const STD: : string & amp;) (* this); }
Const int the sum () {return Pair : : the second (). The sum (); }//by the name of the class and scope resolution operator to invoke the base class method, can use the second () interface, and then the second () returns ArrayInt class object b, b may use the sum () interface (valarray class interface)
Void the Show () the const;
};

Void Wine: : GetBottles ()
{
ArrayInt year (years);
ArrayInt bottle (years);
std::cout <"Enter" & lt; <(const STD: : string & amp;) (* this) & lt; <"The data for" & lt; For (int I=0; i {
std::cout <"Enter year:";
STD: : cin & gt;> Year [I];
std::cout <"Enter bottles for that year:";
STD: : cin & gt;> Bottle [I];
}
Pair : : first ()=year;//by the name of the class and scope resolution operator to invoke the base class method, can use first () interface, then the first () returns a class object ArrayInt, during a class object can accept another ArrayInt
Pair : : the second ()=bottle;
}

Void Wine: : Show () const
{
std::cout <"Wine:" & lt; <(const STD: : string & amp;) (* this) & lt; std::cout <"\ tYear Bottles" & lt; For (int I=0; i {
std::cout <"\ t" & lt; : : first () [I] <"" & lt; : : the second () [I] //by the name of the class and scope resolution operator to invoke the base class method, can use first () interface, then the first () returns a class object ArrayInt, a back can meet [I]
}
}

# endif



//attach the caller

#include
# include "zy2. H"

Int main ()
{
Using STD: : cout;
Using STD: : cin;
Using STD: : endl;

Cout & lt; <"Enter the name of wine:";
Char lab [50].
Cin. Getline (lab, 50);
Cout & lt; <"Enter the number of years:";
Int yrs.
Cin & gt;> Yrs.

Wine holding (lab, yrs);
Holding. GetBottles ();
Holding the Show ();

Const int YRS=3;
Int y [YRS]={1993199, 5199, 8};
48,60,72 int [YRS] b={};
Wine more (" Gushing Grape Red ", YRS, y, b);
More. The Show ();
Cout & lt; <"Total bottles for" & lt; Cout & lt; <"Bye \ n";
}

CodePudding user response:

STD: : string (l), years (y), Pair (ArrayInt (yr, y), ArrayInt (bot, y)) {}
//write PairArray (ArrayInt (yr, y), ArrayInt (bot, y)) compiler can accept,
But such writing PairArray (ArrayInt (yr, years), ArrayInt (bot, years)) compiler is not acceptable. Great god teach genuflect is begged

You stick error directly, the code for a long, looked tired,

This is in the constructor, PairArray as the parent class structure, yr, y, bot, y is the constructor parameters, so no problem,
Years for members of the subclass, the process of the structure of the c + + is to structure the parent class to subclass first, initialization list prior to the constructor of a class, in the initialization list in subclasses member to construct the superclass is wrong,

  • Related