矢量 [英] vectors

查看:51
本文介绍了矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

附加程序很好,但我需要为每个

AcctExample对象创建向量。我知道我可以做到以下几点:


vector< AcctExample>制作AcctExample对象矢量的示例

称为示例。现在,我如何为每个对象激活方法?

另外,为什么我们用向量替换模板? (这就是

分配所需的。我不知道为什么我们这样做。)


ACCTEXAMPLE.CPP

#include< conio.h>

#include< iostream>

#include< fstream>

#include" AcctExample .h" //必须包含此文件或我们获得

undefined

//参考


/ *此文件它没有模板功能。它用于演示多文件程序中的一些C ++

**基础知识。此文件包含

的函数

** DECLARED在同名的.h文件中。 .h文件的类别为

DEFINITION

**,功能为DECLARATIONS。* /


///// ////////////////////////////////////////////////// /////////////////////////

//此课程是公开的,用于例外

处理/////////////////////////
$ b $ // ////////////// ////////////////////////////////////////////////// ////////////////

//类AnError

// {

//} ;

///////////////////////////////////////// ///////////////////////////////////////

//返回帐户余额::使用范围操作员

这里/////////////////////

//// ////////////////////////////////////////////////// //////////////////////////

int AcctExample :: getBalance()

{

返回acctBalance;


}

////////////////// ////////////////////////////////////////////////// ////////////

//持有存款金额。如果输入负数,则

例外是

//thrown.///////////////////// //////////////////////////////////////////////////


int AcctExample :: deposit()

{

int depAmt;

cout< < "请输入存款金额;

cin>> depAmt;

if(depAmt< = 0)

抛出AnError();

返回acctBalance + depAmt;


}

/////////////////////////////////// /////////////////////////////////////////////

//此方法为withAmount取一个int并返回它

acctBalance ///////

////////// ////////////////////////////////////////////////// ////////////////////

int AcctExample :: withdraw(int withAmount)

{

返回acctBalance - withAmount;

}

//////////////////////// ////////////////////////////////////////////////// //////

//这仅仅设定了

余额///////////////////// /////////////////////////////
$ b $ // ///////////// ////////////////////////////////////////////////// /////////////////


void AcctExample :: setBalance(int bal)

{

acctBalance = bal;

}

//////////////////////// ///////////////////////////// ///////////////////////////

//此功能用于显示返回

信息/////////////////////////////

////////// ////////////////////////////////////////////////// ////////////////////


无效AcctExample :: displayBalance()

{

cout<< acctBalance<< PIN;

}

/////////////////////////////// /////////////////////////////////////////////////


ACCTEXAMPLE.H

#include< conio.h>

#include< iostream>

#include< fstream>


using namespace std;

// template< class T>

class AcctExample

{

私人:

字符串fname,lname;

int PIN;

int acctBalance;


public:

// AcctExample(); //默认构造函数

AcctExample(int,int) ; //构造函数

AcctExample():acctBalance(0),PIN(1234){} //这个

初始化

//构造函数默认情况下使acctBalance 0和PIN 1234


AcctExample(AcctExample&); //复制构造函数

//////////// ////////////////////////////////////////////////// //////////////////

class AnError

{

};

////////////////////////////////// //////////////////////////////////////////////

int getBalance();


//返回账户余额

/////////////// ////////////////////////////////////////////////// ///////////////


int deposit();


//返回帐户余额加上存入的金额

//如果金额为0或更少,则会抛出错误

////////////// ////////////////////////////////////////////////// ////////////////

int withdraw(int withAmount);


//返回账户余额减去取款金额

/////////////////////////////////////// /////////////////////////////////////////


void setBalance(int bal);

//将余额设置为你想要的金额

////////////// ////////////////////////////////////////////////// ////////////////


void displayBalance();


//显示结果余额和密码ber

///////////////////////////////////////// ///////////////////////////////////////

};


MAIN.CPP

#include< conio.h>

#include< iostream>

#include< fstream>

#include< vector>

#include" AcctExample.h"


使用命名空间std;

int main()


{

试试

{

int loop;

do

{

AcctExample示例;

// vector< AcctExample> ;示例; //创建一个向量

AcctExample对象

//激活每个对象的方法,并分配每个

已完成

//作为向量成员的对象

example.getBalance();

example.setBalance(example.deposit());

example.getBalance();

example.displayBalance();

ofstream outFile(" BKAccount.txt");

outFile<< example.getBalance();

system(" PAUSE");

cout<< 你想重复交易吗?;

cin>>循环;

}


while(循环!= 1);

}


catch(AcctExample :: AnError)

{

cout<< 有一个错误!\ n";

cout<< 存款金额必须超过0美元;

系统(暂停);

}

}

The attached program is fine, but I need to create vectors for each
AcctExample object. I know that I can do the following:

vector<AcctExample> example which makes a vector of AcctExample objects
called example. Now, how do I activate the methods for each object?
Also, why are we replacing templates with vectors? (That was what the
assignment required. I don''t get why we do this).

ACCTEXAMPLE.CPP
#include <conio.h>
#include <iostream>
#include <fstream>
#include "AcctExample.h"//this file must be included or we get an
undefined
//reference

/*This file has no template functions in it. It''s used to demonstrate
some C++
**basics in a multifile program. This file contains the functions that
were
**DECLARED in the .h file of the same name. The .h file has the class
DEFINITION
**with the function DECLARATIONS.*/

////////////////////////////////////////////////////////////////////////////////
//This class is public and used for exception
handling./////////////////////////
////////////////////////////////////////////////////////////////////////////////
// class AnError
//{
//};
////////////////////////////////////////////////////////////////////////////////
//Returns the account balance :: Scoping operator used
here/////////////////////
////////////////////////////////////////////////////////////////////////////////
int AcctExample::getBalance()
{
return acctBalance;

}
////////////////////////////////////////////////////////////////////////////////
//This holds the deposit amount. If a negative number is entered an
exception is
//thrown.///////////////////////////////////////////////////////////////////////

int AcctExample::deposit()
{
int depAmt;
cout << " Please enter deposit amount";
cin >> depAmt;
if (depAmt <= 0)
throw AnError();
return acctBalance + depAmt;

}
////////////////////////////////////////////////////////////////////////////////
//This method takes an int for withAmount and returns it with
acctBalance///////
////////////////////////////////////////////////////////////////////////////////
int AcctExample::withdraw(int withAmount)
{
return acctBalance - withAmount;
}
////////////////////////////////////////////////////////////////////////////////
//This merely sets the
balance//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void AcctExample::setBalance(int bal)
{
acctBalance = bal;
}
////////////////////////////////////////////////////////////////////////////////
//This function is used to display back
information/////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void AcctExample::displayBalance()
{
cout << acctBalance << PIN;
}
////////////////////////////////////////////////////////////////////////////////

ACCTEXAMPLE.H
#include <conio.h>
#include <iostream>
#include <fstream>

using namespace std;
//template <class T>
class AcctExample
{
private:
string fname, lname;
int PIN;
int acctBalance;

public:
//AcctExample();//default constructor
AcctExample(int, int);//constructor
AcctExample(): acctBalance(0), PIN(1234){}//This
intializing
//constructor makes acctBalance 0 and PIN 1234 by default

AcctExample(AcctExample&);//copy constructor
////////////////////////////////////////////////////////////////////////////////
class AnError
{
};
////////////////////////////////////////////////////////////////////////////////
int getBalance();

//returns the account balance
////////////////////////////////////////////////////////////////////////////////

int deposit();

//returns the account balance plus whatever amount was deposited
//if an amount of 0 or less is deposited an error is thrown
////////////////////////////////////////////////////////////////////////////////
int withdraw(int withAmount);

//returns the account balance minus the amount withdrawn
////////////////////////////////////////////////////////////////////////////////

void setBalance(int bal);
//sets the balance to whatever amount you wish
////////////////////////////////////////////////////////////////////////////////

void displayBalance();

//displays the resulting balance and PIN number
////////////////////////////////////////////////////////////////////////////////
};

MAIN.CPP
#include <conio.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "AcctExample.h"

using namespace std;
int main()

{
try
{
int loop;
do
{
AcctExample example;
//vector<AcctExample> example;//create a vector of
AcctExample objects
//activate the methods for each object, and assign each
completed
//object as a vector member
example.getBalance();
example.setBalance(example.deposit());
example.getBalance();
example.displayBalance();
ofstream outFile("BKAccount.txt");
outFile << example.getBalance();
system("PAUSE");
cout << "Would you like to repeat the transaction?";
cin >> loop;
}

while(loop != 1);
}

catch(AcctExample::AnError)
{
cout << "There was an error!\n";
cout << "Deposit amounts must be more than 0 dollars";
system("PAUSE");
}
}

推荐答案

nick写道:
附加程序很好,但我需要为每个创建矢量
AcctExample对象。我知道我可以做到以下几点:

vector< AcctExample>制作一个AcctExample对象矢量的例子叫做例子。现在,如何为每个对象激活方法?


可能使用表格


示例[index] .method(参数);

另外,为什么我们用矢量替换模板吗? (这就是
任务所需要的。我不知道为什么我们这样做)。


怎么会_we_知道?询问给你

作业的人。

[..]
The attached program is fine, but I need to create vectors for each
AcctExample object. I know that I can do the following:

vector<AcctExample> example which makes a vector of AcctExample objects
called example. Now, how do I activate the methods for each object?
Probably by using the form

example[index].method(arguments);
Also, why are we replacing templates with vectors? (That was what the
assignment required. I don''t get why we do this).
How the hell should _we_ know? Ask the person who gave you the
assignment.
[..]



nick写道:
另外,为什么我们用向量替换模板? (这就是
任务所需的。我不知道为什么我们这样做。)
Also, why are we replacing templates with vectors? (That was what the
assignment required. I don''t get why we do this).




也许我只是错过了它,但是我没有看到模板的使用方式/

首先需要...你的意思是用

向量替换数组吗?向量和模板是两个完全独立的东西

(向量USE模板,但向量不能代替

模板。)


你账户类中的任何内容似乎都不需要变量类型。



Maybe I''m just missing it, but I fail to see how templates were used /
required in the first place... Did you mean replacing arrays with
vectors? Vectors and templates are two completely seperate things
(Vectors USE templates, but vectors are not a substitute for
templates.)

Nothing in your accounts class seems to require a varying variable type.


这可能是点......矢量使用模板。我只是想把

把所有这些信息都记在我脑海里.....谢谢。

That''s probably the point......vectors use templates. I''m just trying
to get all this information into my head.....thanks.


这篇关于矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆