Migrating from C # to C ++, good links?

I just finished my second OOP class, and both of my first and second classes were taught in C #, but for the rest of my programming classes we will work in C ++. In particular, we will use the Visual C ++ compiler. The problem is that we ourselves must learn the basics of C ++, without going from our professor. So, I was interested to know if you know any of the good resources that I could learn to learn about C ++, coming from the background of C #?

I ask about this because I noticed a lot of differences between C # and C ++, for example, declaring your main int method and returning 0, and that your main method is not always contained in the class. In addition, I noticed that all your classes should be written before your main method, I heard that this is due to the fact that VC ++ matches from top to bottom?

Anyway, do you know about good links?

Thanks! (Also, is there somewhere where I can compare simple programs that I wrote in C # to see how they would look in C ++, for example, the one I wrote below)?

 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
source share
6 answers

C ++ and C # are different languages, with different semantics and rules. There is no complicated and quick transition from one to another. You will need to learn C ++.

To do this, some of the resources for learning C ++ questions may give you interesting suggestions. Also find books in C ++ โ€” for example, the final guide and a list of books in C ++ .

In any case, it will take you a long time to learn C ++. If your teacher expects that you simply know C ++ because of blue, your school has a serious problem.

+7
source

My advice is this:

Do not think about C ++ in terms of C #. Start from scratch and try learning C ++ from a good, solid C ++ tutorial.

Trying to map C ++ to C # thinking won't make you a good C ++ developer - it really is very different. It is very difficult to port from C # to C ++ (not to C ++ / CLI, but the C ++ Strait), because most of C # is really a .NET platform that will not be available when working in C ++.

In addition, there are so many things that do things differently in C ++ than how you did it in C #. Since C ++ templates are similar to C ++ templates, they are very different, and this change really becomes pervasive when working in C ++.

Your experience will facilitate a lot, but itโ€™s better to think of it as what it is - a completely different language.

+6
source

Actually there is no โ€œplaceโ€ of comparison that I know for checking your code for its implementation in C ++. Instead, I would recommend that you find a local OO development specialist and ask him or her to conduct a thorough code review of your sample code. After that, he or she will probably go through the port in C ++ and you will see that the languages โ€‹โ€‹are not much different. (In libraries you will find significant differences, not languages, but this may not be a significant difference for you at the moment.)

A code review is different than asking your professional or TP to rate your work. The professor will likely not have time to conduct a full review with you (although he or she can go through a few points in his office.) A good code reviewer will guide you through the code line by line, providing feedback and questions such as โ€œwhy do you think that I would either not do this? ", or" did you think what would happen if your requirements change to X? " or "what happens to this code if someone else wants to reuse Y?" When you go through the review, they will help strengthen the good principles of OO. You will learn a lot from someone who made a lot of them. (Finding a good person can be a difficult task - if necessary, get a referral or ask someone who is familiar with refactoring. Perhaps ask a TA or a cooling student to do this.) With your code above, I would expect it to take an hour or two.

You can also ask StackOverflow residents to view the code with you via email or right here in the comments / responses. I am sure that you will receive vivid answers, and the discussion is likely to be educational.

It is also helpful to review the code for the test code - ask questions about โ€œwhy did you do this?โ€. may lead to insight; and itโ€™s always fun when a beginner finds an error in the code of the guru.

After you conduct a review with an expert to find out what they are (at least one or two reviews), think about doing code reviews with your colleagues in the class, where each of them looks at a different code. Even an inexperienced set of eyes may ask a question that raises some deep understanding.

I understand that you are considering a C ++ class, but you also need more practice with the basics of OO design. After you have more hands-on experience, you will find that all C-based languages โ€‹โ€‹are just syntactic variations of the general set of ideas, although some language functions make your work easier and some more difficult.

+1
source

Many colleges and universities teach C # / Java either before or even instead of C ++. They do this because there is a belief that you will learn how to correctly orient objects in a language that will not allow you to use approaches other than OO that are still possible in C ++.

You should just ask your professor if this is the case.

If so, you should find that he / she will introduce C ++ specifics gently and only where necessary. The goal is that you can apply many of the good practices that you learned in C # to your C ++ code. You will also see more clearly where C ++ allows you to break some OO rules and, more importantly, where it can even be useful.

Alternatively, if you find that your professor jumps right into C ++ pointers and memory management without any guidance, then (as someone else said) the course does not look very well planned. In this case, you may need to get a copy of the C ++ programming language from Stroustrup.

+1
source

The main thing is to always keep in mind that in C # when transferring an object, you actually pass a link to this object.

C #: void EnrollStudent( Student s ) s is a reference (location in memory) to the actual object.

C ++: void EnrollStudent( Student s ) s is the actual object. As big as an object is, how much space will you occupy on the stack.

C ++ equivalent for C # void EnrollStudent( Student* s ) method

-one
source

All Articles