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

Slides:



Advertisements
Similar presentations
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Advertisements

Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Basic Elements of C++ Chapter 2.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Learners Support Publications Classes and Objects.
Fundamental Programming: Fundamental Programming Introduction to C++
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
CPSC 252 Operator Overloading and Convert Constructors Page 1 Operator overloading We would like to assign an element to a vector or retrieve an element.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSE 332: C++ template examples Today: Using Class and Function Templates Two examples –Function template for printing different types –Class template for.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Procedural and Object-Oriented Programming
LESSON 06.
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Engineering Problem Solving With C An Object Based Approach
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Eugene Hsu.
CS1010 Programming Methodology
Motivation and Overview
Andy Wang Object Oriented Programming in C++ COP 3330
Basic Elements of C++.
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Objectives Identify the built-in data types in C++
A Lecture for the c++ Course
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Chapter 5 Classes.
Pointers and Pointer-Based Strings
Basic Elements of C++ Chapter 2.
(5 - 1) Object-Oriented Programming (OOP) and C++
Dynamic Memory Allocation Reference Variables
User-defined Functions
Chapter 2 – Getting Started
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
Classes and Data Abstraction
Operator Overloading; String and Array Objects
Brought to you by C++ Tutorial Brought to you by
More About Data Types & Functions
User-defined Functions
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Functions Pass By Value Pass by Reference
Classes and Objects.
(5 - 1) Object-Oriented Programming (OOP) and C++
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Defining Classes and Methods
Pointers and dynamic objects
Using string type variables
Standard Version of Starting Out with C++, 4th Edition
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Class rational part2.
CECS 130 Final Exam Review Session
Creating and Using Classes
Introduction to Classes and Objects
Presentation transcript:

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

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++

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() { }

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; }

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) { }

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) { }

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.

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) { }

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) { }

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.

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...

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.

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

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

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.

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...