gpt4 book ai didi

c# - 从 C# 过渡到 C++,好的引用资料?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:03:23 25 4
gpt4 key购买 nike

我刚刚完成了我的第二门 OOP 类(class),我的第一门和第二门类(class)都是用 C# 教授的,但对于我的其余编程类(class),我们将使用 C++。具体来说,我们将使用可视化 C++ 编译器。问题是,我们需要自己学习 C++ 的基础知识,而不是从我们的教授那里过渡。所以,我想知道你们中是否有人知道一些我可以去学习 C++ 的好资源,来自 c# 背景?

我问这个是因为我注意到 C# 和 C++ 之间的许多差异,例如将 main 方法声明为 int,并返回 0,并且您的 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();

}
}

最佳答案

C++ 和 C# 是不同的语言,具有不同的语义和规则。没有从一个切换到另一个的硬性快速方法。您将必须学习 C++。

为此,一些 resources to learn C++问题可能会给你有趣的建议。还搜索 C++ books - 查看 definitive C++ book guide and list ,例如。

无论如何,您都需要大量时间来学习 C++。 如果你的老师希望你突然知道 C++,那么你的学校就有严重的问题了。

关于c# - 从 C# 过渡到 C++,好的引用资料?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1904626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com