4

2番目のOOPクラスを完了したばかりで、1番目と2番目のクラスは両方ともC#で教えられましたが、残りのプログラミングクラスでは、C++で作業します。具体的には、ビジュアルC++コンパイラーを使用します。問題は、教授からの移行なしに、C++の基本を自分で学ぶ必要があるということです。それで、ac#のバックグラウンドから来て、C ++について学ぶために私が行くことができるいくつかの良いリソースを知っている人がいるかどうか疑問に思いましたか?

メソッドintの宣言や0の返しなど、C#とC ++の間に多くの違いがmainあり、メインメソッドが常にクラスに含まれているとは限らないことに気付いたので、これを尋ねます。また、すべてのクラスをメインメソッドの前に作成する必要があることに気付きました。これは、VC++がトップダウンで準拠しているためだと聞きました。

とにかく、あなたはいくつかの良い参考文献を知っていますか?

ありがとう!(また、C#で作成した単純なプログラムを比較して、以下のようにC ++でどのように表示されるかを確認できる場所はありますか?)

using System;
using System.IO;
using System.Text; // for stringbuilder

class Driver
{
const int ARRAY_SIZE = 10;

static void Main()
{
    PrintStudentHeader(); // displays my student information header

    Console.WriteLine("FluffShuffle Electronics Payroll Calculator\n");
    Console.WriteLine("Please enter the path to the file, either relative to your current location, or the whole file path\n");

    int i = 0;

    Console.Write("Please enter the path to the file: ");
    string filePath = Console.ReadLine();

    StreamReader employeeDataReader = new StreamReader(filePath);

    Employee[] employeeInfo = new Employee[ARRAY_SIZE];

    Employee worker;

    while ((worker = Employee.ReadFromFile(employeeDataReader)) != null)
    {
        employeeInfo[i] = worker;
        i++;
    }

    for (int j = 0; j < i; j++)
    {
        employeeInfo[j].Print(j);
    }

    Console.ReadLine();

}//End Main()


class Employee
{
const int TIME_AND_A_HALF_MARKER = 40;
const double FEDERAL_INCOME_TAX_PERCENTAGE = .20;
const double STATE_INCOME_TAX_PERCENTAGE = .075;
const double TIME_AND_A_HALF_PREMIUM = 0.5;

private int employeeNumber;
private string employeeName;
private string employeeStreetAddress;
private double employeeHourlyWage;
private int employeeHoursWorked;
private double federalTaxDeduction;
private double stateTaxDeduction;
private double grossPay;
private double netPay;

// -------------- Constructors ----------------

public Employee() : this(0, "", "", 0.0, 0) { }

public Employee(int a, string b, string c, double d, int e)
{
    employeeNumber = a;
    employeeName = b;
    employeeStreetAddress = c;
    employeeHourlyWage = d;
    employeeHoursWorked = e;
    grossPay = employeeHourlyWage * employeeHoursWorked;

    if (employeeHoursWorked > TIME_AND_A_HALF_MARKER)
        grossPay += (((employeeHoursWorked - TIME_AND_A_HALF_MARKER) * employeeHourlyWage) * TIME_AND_A_HALF_PREMIUM);

    stateTaxDeduction = grossPay * STATE_INCOME_TAX_PERCENTAGE;
    federalTaxDeduction = grossPay * FEDERAL_INCOME_TAX_PERCENTAGE;
}

// --------------- Setters -----------------

/// <summary>
/// The SetEmployeeNumber method
/// sets the employee number to the given integer param
/// </summary>
/// <param name="n">an integer</param>
public void SetEmployeeNumber(int n)
{
    employeeNumber = n;
}

/// <summary>
/// The SetEmployeeName method
/// sets the employee name to the given string param
/// </summary>
/// <param name="s">a string</param>
public void SetEmployeeName(string s)
{
    employeeName = s;
}

/// <summary>
/// The SetEmployeeStreetAddress method
/// sets the employee street address to the given param string
/// </summary>
/// <param name="s">a string</param>
public void SetEmployeeStreetAddress(string s)
{
    employeeStreetAddress = s;
}

/// <summary>
/// The SetEmployeeHourlyWage method
/// sets the employee hourly wage to the given double param
/// </summary>
/// <param name="d">a double value</param>
public void SetEmployeeHourlyWage(double d)
{
    employeeHourlyWage = d;
}

/// <summary>
/// The SetEmployeeHoursWorked method
/// sets the employee hours worked to a given int param
/// </summary>
/// <param name="n">an integer</param>
public void SetEmployeeHoursWorked(int n)
{
    employeeHoursWorked = n;
}

// -------------- Getters --------------

/// <summary>
/// The GetEmployeeNumber method
/// gets the employee number of the currnt employee object
/// </summary>
/// <returns>the int value, employeeNumber</returns>
public int GetEmployeeNumber()
{
    return employeeNumber;
}

/// <summary>
/// The GetEmployeeName method
/// gets the name of the current employee object
/// </summary>
/// <returns>the string, employeeName</returns>
public string GetEmployeeName()
{
    return employeeName;
}

/// <summary>
/// The GetEmployeeStreetAddress method
/// gets the street address of the current employee object
/// </summary>
/// <returns>employeeStreetAddress, a string</returns>
public string GetEmployeeStreetAddress()
{
    return employeeStreetAddress;
}

/// <summary>
/// The GetEmployeeHourlyWage method
/// gets the value of the hourly wage for the current employee object
/// </summary>
/// <returns>employeeHourlyWage, a double</returns>
public double GetEmployeeHourlyWage()
{
    return employeeHourlyWage;
}

/// <summary>
/// The GetEmployeeHoursWorked method
/// gets the hours worked for the week of the current employee object
/// </summary>
/// <returns>employeeHoursWorked, as an int</returns>
public int GetEmployeeHoursWorked()
{
    return employeeHoursWorked;
}

// End --- Getter/Setter methods

/// <summary>
/// The CalcSalary method
/// calculates the net pay of the current employee object
/// </summary>
/// <returns>netPay, a double</returns>
private double CalcSalary()
{
    netPay = grossPay - stateTaxDeduction - federalTaxDeduction;

    return netPay;
}

/// <summary>
/// The ReadFromFile method
/// reads in the data from a file using a given a streamreader object
/// </summary>
/// <param name="r">a streamreader object</param>
/// <returns>an Employee object</returns>
public static Employee ReadFromFile(StreamReader r)
{
    string line = r.ReadLine();

    if (line == null)
        return null;

    int empNumber = int.Parse(line);
    string empName = r.ReadLine();
    string empAddress = r.ReadLine();
    string[] splitWageHour = r.ReadLine().Split();
    double empWage = double.Parse(splitWageHour[0]);
    int empHours = int.Parse(splitWageHour[1]);

    return new Employee(empNumber, empName, empAddress, empWage, empHours);
}

/// <summary>
/// The Print method
/// prints out all of the information for the current employee object, uses string formatting
/// </summary>
/// <param name="checkNum">The number of the FluffShuffle check</param>
public void Print(int checkNum)
{
    string formatStr = "| {0,-45}|";

    Console.WriteLine("\n\n+----------------------------------------------+");
    Console.WriteLine(formatStr, "FluffShuffle Electronics");
    Console.WriteLine(formatStr, string.Format("Check Number: 231{0}", checkNum));
    Console.WriteLine(formatStr, string.Format("Pay To The Order Of:  {0}", employeeName));
    Console.WriteLine(formatStr, employeeStreetAddress);
    Console.WriteLine(formatStr, string.Format("In The Amount of {0:c2}", CalcSalary()));
    Console.WriteLine("+----------------------------------------------+");
    Console.Write(this.ToString());
    Console.WriteLine("+----------------------------------------------+");
}

/// <summary>
/// The ToString method
/// is an override of the ToString method, it builds a string
/// using string formatting, and returns the built string. It particularly builds a string containing
/// all of the info for the pay stub of a sample employee check
/// </summary>
/// <returns>A string</returns>
public override string ToString()
{
    StringBuilder printer = new StringBuilder();

    string formatStr = "| {0,-45}|\n";

    printer.AppendFormat(formatStr,string.Format("Employee Number:  {0}", employeeNumber));
    printer.AppendFormat(formatStr,string.Format("Address:  {0}", employeeStreetAddress));
    printer.AppendFormat(formatStr,string.Format("Hours Worked:  {0}", employeeHoursWorked));
    printer.AppendFormat(formatStr,string.Format("Gross Pay:  {0:c2}", grossPay));
    printer.AppendFormat(formatStr,string.Format("Federal Tax Deduction:  {0:c2}", federalTaxDeduction));
    printer.AppendFormat(formatStr,string.Format("State Tax Deduction:  {0:c2}", stateTaxDeduction));
    printer.AppendFormat(formatStr,string.Format("Net Pay:    {0:c2}", CalcSalary()));

    return printer.ToString();

}
}
4

6 に答える 6

7

C ++とC#は異なる言語であり、セマンティクスとルールが異なります。あるものから別のものに切り替えるための困難で迅速な方法はありません。C++を学ぶ必要があります。

そのために、C ++の質問を学ぶためのリソースのいくつかは、興味深い提案を与えるかもしれません。C ++の本も検索します。たとえば、最も信頼のおけるC++の本のガイドとリストを参照してください。

いずれにせよ、C++を学ぶにはかなりの時間が必要です。先生があなたがC++をすぐに知っていることを期待している場合、あなたの学校は深刻な問題を抱えています。

于 2009-12-15T01:15:58.307 に答える
7

私のアドバイスはこれです:

C ++をC#の観点から考えないでください。ゼロから始めて、優れた堅実なC++教科書からC++を学んでみてください。

C ++をC#の考え方にマッピングしようとしても、優れたC++開発者にはなりません。実際には非常に異なります。C#の多くは実際には.NETフレームワークであり、C ++での作業では使用できないため、C#からC ++(C ++ / CLIではなく、単純なC ++)に移植するのは非常に困難です。

また、C ++では、C#で行う方法とは異なる方法で行われることが非常に多くあります。C#ジェネリックはC ++テンプレートのように見えますが、それらは非常に異なり、C++で作業するときにその変更が実際に広まります。

あなたの経験は多くのことを容易にしますが、それが何であるか、つまり完全に異なる言語であると考える方が本当に良いです。

于 2009-12-15T01:17:28.617 に答える
1

コードをC++実装と照合するために私が知っている比較「場所」は実際にはありません。代わりに私がお勧めするのは、OO開発の地元の専門家を見つけて、サンプルコードの徹底的なコードレビューを彼または彼女に行わせることです。その後、彼または彼女はおそらくあなたと一緒にC ++への移植を行うことができ、言語はそれほど変わらないことがわかります。(ライブラリは、言語ではなく、意味のある違いを見つける場所ですが、現時点では、それはあなたにとって適切な区別ではないかもしれません。)

コードレビューは、教授やTAに作業の採点を依頼することとは異なります。教授はあなたと完全なレビューを行う時間がない可能性があります(彼または彼女は彼らのオフィスでいくつかのポイントを調べることができるかもしれませんが)。優れたコードレビューアはコードを1行ずつ説明し、フィードバックを提供します「なぜ私がこれを行うか、しないと思いますか?」、「要件がXに変更された場合に何が起こるかを検討しましたか?」、「他の誰かが再利用したい場合、このコードはどうなりますか?」などの質問Y?」レビューを進めると、OOの優れた原則を強化するのに役立ちます。あなたはこれらの多くをした誰かから多くの新しいことを学ぶでしょう。(良い人を見つけるのは難しい作業かもしれません-可能であれば紹介を得るか、リファクタリングに精通している人を求めてください。

StackOverflowの住人に、メールまたはコメント/回答でコードレビューを依頼することも検討できます。カラフルな答えが返ってくると思いますし、議論は教育的なものになるでしょう。

経験豊富な人のコードのコードレビューを行うことも価値があります-「なぜこれをしたのですか?」について質問します。洞察につながる可能性があります。初心者が教祖のコードにバグを見つけたときは、いつもとても楽しいです。

専門家によるレビューを行って、彼らがどのようなものかを確認した後(1つか2つのレビューでも)、教室の同僚とコードレビューを行うことを検討してください。そこでは、それぞれが相手のコードをレビューします。経験の浅い目でさえ、より深い理解を引き起こす質問をするかもしれません。

考慮すべきC++クラスワークがあることは承知していますが、オブジェクト指向設計の基礎についてさらに練習する必要もあります。より実践的な経験を積むと、すべてのCベースの言語は、一般的な一連のアイデアの構文上のバリエーションにすぎないことがわかります。言語機能によっては、作業が簡単になるものもあれば、難しくなるものもあります。

于 2009-12-15T05:57:07.017 に答える
1

多くの大学は、C ++の前または代わりにC#/Javaを教えています。これを行うのは、C++でまだ可能な非オブジェクト指向アプローチを使用できない言語でオブジェクト指向開発をより正確に学習できるという信念があるためです。

それが事実であるかどうかあなたはただあなたの教授に尋ねるべきです。

もしそうなら、あなたは彼/彼女がC++の詳細を穏やかにそして必要な場合にのみ紹介することを見つけるべきです。目的は、C#で学習した多くの優れたプラクティスをC++コードに適用できるようにすることです。また、C ++でいくつかのOOルールを破ることができる場所、さらに重要なことに、これが役立つ可能性がある場所をより明確に確認できます。

あるいは、教授がガイダンスなしでC ++ポインタとメモリ管理に直接飛び込んでいることに気付いた場合、(他の誰かが言ったように)コースはあまりよく計画されていないように見えます。この場合、StroustrupによるC++プログラミング言語のコピーの入手を検討する必要があるかもしれません。

于 2009-12-15T06:41:26.840 に答える
0

特定の状況に対応する本: C#開発者向けのPro Visual C ++ 2005

代替テキスト

(また、 MSDNはそれらの間のいくつかの違いに注意します。)

于 2010-01-07T02:49:55.540 に答える
-1

常に覚えておくべき主なことの1つは、C#ではオブジェクトを渡すときに、実際にはそのオブジェクトへの参照を渡すことです。

C#:void EnrollStudent( Student s ) sは、実際のオブジェクトへの参照(メモリ内の場所)です。

C ++:void EnrollStudent( Student s ) sは実際のオブジェクトです。オブジェクトの大きさと同じくらい、それはスタックで占めるスペースの量です。

C#メソッドに相当するC++は次のとおりです。 void EnrollStudent( Student* s )

于 2010-01-07T02:43:24.180 に答える