Presentation is loading. Please wait.

Presentation is loading. Please wait.

From C to C++: Summary of weeks 1 - 4

Similar presentations


Presentation on theme: "From C to C++: Summary of weeks 1 - 4"— Presentation transcript:

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

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 #include <stdio.h> #define MAX 15 int main() { ... } #include <stdio.h> main() { ... } // header file #define MAX 15 // 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() { ... } #include <stdio.h> main() { ... } // header file #define MAX 15 // 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 // main.cpp #include <iostream> using namespace std; #include “Type1.h” #include “Type2.h” int main() { ... } // Type1.h type function (type identifier); // Type2.h type function (type identifier); // 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... // main.cpp #include <iostream> using namespace std; #include “Type1.h” #include “Type2.h” int main() { ... } // Type1.h type function (type identifier); // Type2.h type function (type identifier); // 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++ Derived Type
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 Behaviour Derived Type also describes member functions assessible by an Object of that Derived Type.

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 // 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.h struct Type1 { type member; }; // Type2.h struct Type2 { type 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... // 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.h struct Type1 { type member; }; // Type2.h struct Type2 { type 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 name when 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++ Derived Type
A Derived Type can specify if data or behaviour is accessible outside an object. Data and member functions can be grouped as Private or Public. Modifier Derived Type Data Outside the Object Behaviour Query

14 From C to C++ Derived Type
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)... Modifier Derived Type 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...


Download ppt "From C to C++: Summary of weeks 1 - 4"

Similar presentations


Ads by Google