1 / 16

The Coding Transition

From C to C++: Summary of weeks 1 - 4. The Coding Transition. From C to C++. We have been learning how to program in C++. Over the past 4 weeks, the emphasis of programming has shifted from C to C++

Download Presentation

The Coding Transition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. From C to C++:Summary of weeks 1 - 4 The Coding Transition

  2. From C to C++ • We have been learning how to program in C++. • Over the past 4 weeks, the emphasis of programming has shifted from C to C++ • If you have noticed, the In-Class exercises are demonstrating (in levels) how to shift from C programming to C++. • Let's quickly review this process to date. • The next few slides review visually the steps of the transition of C coding to C++

  3. From C to C++ • We learned to separate code into modules consisting of header and implementation files // header file #define MAX 15 #include <stdio.h> main() { ... } #include <stdio.h> #define MAX 15 int main() { ... } // implementation file #include <stdio.h> #include “header file name” int main() { }

  4. From C to C++ • We learned to process input and output using objects instead of functions #include <iostream> using namespace std; #define MAX 15 int main() { ... } // header file #define MAX 15 #include <stdio.h> main() { ... } // implementation file #include <iostream> using namespace std; #include “header file name” int main() { cin >> x; cout << “x is “ << x << endl; }

  5. From C to C++ • Modules can be grouped by type in order to solve a complex programming problem // Type1.h type function (type identifier); // Type2.h type function (type identifier); // main.cpp #include <iostream> using namespace std; #include “Type1.h” #include “Type2.h” int main() { ... } // Type1.cpp #include <iostream> using namespace std; #include “Type1.h” type function (type identifier) { } // Type2.cpp #include <iostream> using namespace std; #include “Type2.h” type function (type identifier) { }

  6. From C to C++ • Pointers and Arrays are important tools when coding in C++ (they help save memory space). The const keyword is used for protection... // Type1.h type function (type identifier); // Type2.h type function (type identifier); // main.cpp #include <iostream> using namespace std; #include “Type1.h” #include “Type2.h” int main() { ... } // Type1.cpp #include <iostream> using namespace std; #include “Type1.h” type function (type identifier) { } // Type2.cpp #include <iostream> using namespace std; #include “Type2.h” type function (type identifier) { }

  7. From C to C++ • One important term in C++ is Encapsulation. The process of hiding data and behaviour from a program that uses an object unless needed. Derived Type Derived Type describes the member data contained in each Object. Many Instances or Objects can be declared for a Derived Type Data Derived Type also describes member functions assessible by an Object of that Derived Type. Behaviour

  8. From C to C++ • The structure of a Derived Type is declared in a header file, while its member functions are defined in an implementation file // Type1.h struct Type1 { type member; }; // Type2.h struct Type2 { type member; }; // main.cpp #include <iostream> using namespace std; #include “Type1.h” #include “Type2.h” int main() { Type1 objectName; Type2* objectAddress = &objectName; ... cout << objectName.member; cout << objectAddress->member; } // Type1.cpp #include “Type1.h” int Type::function(type identifier) { } // Type2.cpp #include “Type2.h” int Type::function(type identifier) { }

  9. From C to C++ • Modules can access members from instances by using dot notation or arrow notation... // Type1.h struct Type1 { type member; }; // Type2.h struct Type2 { type member; }; // main.cpp #include <iostream> using namespace std; #include “Type1.h” #include “Type2.h” int main() { Type1 objectName; Type2* objectAddress = &objectName; ... cout << objectName.member; cout << objectAddress->member; } // Type1.cpp #include “Type1.h” int Type::function(type identifier) { } // Type2.cpp #include “Type2.h” int Type::function(type identifier) { }

  10. From C to C++ • There are numerous ways to pass up parameters to functions. • The most convenient method (thus reducing lengthy parameter lists) is to pass up an instance itself. • Passing up the instance allows access to its members. • Two common methods are called:Pass by Value and Pass by Address.

  11. From C to C++ • To reduce duplication in computer memory, it is more efficient to Pass by Address. • When passing a parameter by address, you need to be aware and review all of the subtle rules associated: • Possibly use const keyword for protection • Parentheses around instance namewhen dereferencing if using dot notation. • Using alternatives (such as Pass by Reference) to simply your coding...

  12. From C to C++ • In addition, you can declare an array of instances and refer to an element of the array as opposed to the array (so you can use loops, etc...) • In C++ you are learning the syntax and rules for properly using these instances and passing by address. It is strongly recommended to review these rules regularly.

  13. From C to C++ • A Derived Type can specify if data or behaviour is accessible outside an object. Data and member functions can be grouped asPrivate or Public. Derived Type Modifier Data Outside the Object Behaviour Query

  14. From C to C++ • By providing the option to hide data and/or behaviour of an instance of an object ties in the purpose of Object Oriented programming and how it relates to other languages (like Java)... Derived Type Modifier Data Outside the Object Behaviour Query

  15. From C to C++ • Other lessons have provided “additional” rules for programming in C++ (such as commenting, forward declarations, declaring variables“on-the-fly”, and input/output formatting options. • In a course such as this, it is important to practice writing C++ programs... • Just looking at course notes may not prepare you to perform assignments, or write quizzes and tests.

  16. From C to C++ • How can I get hands-on practice? • Do / Redo In-Class Exercises • Do the Workshop Problems • View Instructor's sample code and understand what code does. • Try to code your own examples • Work on your assignment • In assignment #2 you will convert your assignment #1 to C++ code...

More Related