RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

Size: px
Start display at page:

Download "RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!"

Transcription

1 1

2 2 RECOMMENDATION Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function! 2

3 Function Overloading C++ provides the capability of Using The Same Function Name For More Than One Function (function overloading). Since the Compiler Must Be Able to Determine Which Function To Use Based on the Number & Data Types of the Parameters, the Function Signatures of each function Must Be Unique. 3

4 We Could Create Function Max (int No1, int No2) Coding Might Be As Follows int Max (int No1, int No2) { int NewMax; if(no1 >= No2) NewMax = No1; else NewMax = No2; return (NewMax); } Main int main(int argc, char * argv[] ) { printf ("Max (5, -7) = %d\n", Max (5, -7)); getchar(); return(0); } 4 4

5 We Could Also Create Function Max (double No1, double No2) Coding The Function Overload Might Be As Follows:Max double Max (double No1, double No2) { double NewMax; if(no1 >= No2) NewMax = No1; else NewMax = No2; return (NewMax); } Main int main(int argc, char * argv[] ) { printf ("Max (5, -7) = %d\n", Max (5, -7)); printf ("Max (5.5, 2.9) = %f\n", Max (5.5, 2.9)); getchar(); return(0); } 5 Warning: creating overloaded functions with identical parameter lists and different return types is a syntax error!! 5

6 Function Signature The Signatures Of Each Function Overload Must Be Different! Signature 1 Max(int, int) Signature 2 Max(double, double) "Function overloading is pretty unique to C++"; it is not supported in standard C or most other languages. WIKI But There are a number of languages that do support Function Overloading: Java, Python, Scala, etc. 6

7 7

8 Simple Aggregate Arrays, Structs, Classes Aggregate A Collection Of Objects Stored As One Unit! C/C++ The array is an Aggregate! C/C++ The Struct is an Aggregate! C++ The Class is an Aggregate! 8

9 9

10 10

11 11

12 Each & Every C++ File You Submit This Semester Must Include A Completed Documentation Block At The Top ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // // // main.cpp // // // // Purpose : // // // // // // Written By : Thomas E. Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// It Is Time We Update This Documentation Block! 12

13 Add The Following Includes To The Top Of Your Program ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // // // Main.cpp // // // // Purpose : Create and test a Student Class // // This program will demonstrate the includes, defines, class // // definitions, prototypes, all class methods. Included will be // // constructors, destructors, set, display, operator overloads, // // etc. // // // // Written By : Thomas E. Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Includes # include "Utilities.hpp" COMPILE THE PROGRAM! 13

14 Add The Following Defines To The Top Of Your Program ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // // // Main.cpp // // // // Purpose : Create and test a Student Class // // This program will demonstrate the includes, defines, class // // definitions, prototypes, all class methods. Included will be // // constructors, destructors, set, display, operator overloads, // // etc. // // // // Written By : Thomas E. Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Includes # include "Utilities.hpp" COMPILE THE PROGRAM! // Defines # define MALE false # define FEMALE true # define STUDENT_DIAGNOSTIC_LEVEL 1 14

15 We Want To Create A Class That Is Equivalent To Struct Student struct Student { char Name [20]; }; // Set To Be Empty String long No; // Set To Be 0 bool Gender; // Set To MALE 15

16 16

17 Class Student No Methods/Function & No Data // Includes # include "Utilities.hpp" // Defines # define MALE false # define FEMALE true # define STUDENT_DIAGNOSTIC_LEVEL 1 COMPILE THE PROGRAM! // Classes & Structs class Student { }; 17

18 int main (int argc, char * argv[]) { Add The Following Class To main COMPILE THE PROGRAM! puts (" "); puts (" "); puts ("++ Class Student Added To Main ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks YOUR NAME ++"); puts (" "); puts (" \n"); puts (" Start Of Main \n"); } { } Student John; puts (" HitCarriageReturnToContinue(); return (0); End Of Main \n"); Does Not Do Much Yet! 18

19 Instantiation Instantiation is one of the most important mechanisms for code reuse in object-oriented programming languages; instantiation of a class is the creation of a new instance of that class. Programmers can define object classes, define their own kinds of objects, and instantiate them as needed. This is one of the most important mechanisms for code reuse in objectoriented programming languages. Student John; Instantiates A Student Type Object Called John it is pretty much an empty object at this point but we will change all that! 19

20 20

21 Classes Are All About Security Add public & private! class Student { public: COMPILE THE PROGRAM! Most Classes Will Contain A Public Portion! The public portion will include constructors, destructors, and other functions/methods that we want the world to access. Most Classes Will Contain A Private Portion! }; private: The private portion will often include the data members and functions that we do not want to be public. As a general rule, we would like to very rigidly control access to the data! 21

22 Add The Student Data Members class Student { public: COMPILE THE PROGRAM! Only Student Methods/Functions, And Friends Of This Class, Will Be Able To Access Private Data Members Name, No, & Gender! We Will Study "Friends" Later! private: }; char Name [20]; long No; // Set To Be 0 bool Gender; // Set To MALE // Set To Be Empty String 22

23 puts (" Try The Following In main Start Of Main \n"); Student John; John.No = 1232; Delete This Line Of Code! COMPILE THE PROGRAM! puts (" End Of Main \n"); Note The Underline Indicating A Problem! 23

24 24

25 class Student { public: Student (void); }; About Constructors The Constructor is a Function Whose Name Is The Same As The Class It Is The Constructors Responsibility To Do All That Is Necessary To Initialize/Assign The Data Members. The Constructor Will Most Often Be In The Public Section Of The Class.. The Constructor Will Often Have Parameters, But Not Always. 25

26 class Student { public: Student (void); }; About Constructors - 2 When an Object of a Class is Created, C++ Automatically Calls the Constructor for that class. If No Constructor Is Defined, C++ creates & invokes a default constructor, which allocates memory for the object, but doesn't initialize it. (Seldom Used) A Class may have multiple Constructors; this function can be OVERLOADED! 26

27 Add The Student Constructor Prototype class Student { public: Student(void); private: char Name [20]; long No; bool Gender; }; COMPILE THE PROGRAM! Linker Error The compiler cannot find a function, called Student Maybe It Does Not Exist YES Maybe It Does Not Exist In The Way We Called It arguments don't match 27

28 Add Code For The Student Constructor Below main We Already Have The Prototype In The Class Student Each & Every Class Method/Function Will Include The Name Student::Student(void) Student:: ) { Of The Class (Student) & The Scope Operator ( :: ) After The Scope Operator ( :: ) Will Be The Name Of The Function Constructor Is Name Of Class Inside The Parenthesis Are The Arguments Passed None This Time } COMPILE THE PROGRAM! 28

29 Finish Student(void) The Constructor Is To Do All That Is Necessary To Create A New Object Never Leave Garbage?? Data Student::Student(void) { } if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student(void)"); strcpy_s(name, ""); No = 0; COMPILE THE PROGRAM! Gender = MALE; COMPILE THE PROGRAM AGAIN! It Compiles, But That Does Not Make It Correct Doubting Thomas I Don't Even Know That It Was Executed! Doubting Thomas 29

30 30

31 Add The Student Destructor Prototype class Student { public: Student(void); ~Student(void); private: char Name [20]; long No; bool Gender; }; COMPILE THE PROGRAM! Linker Error The compiler cannot find a function, called ~Student Every Class Has A Destructor. If You Do Not Add A Class Destructor, The Compile Builds One - Which Does Nothing - During Compilation. If You Include The Prototype For The Destructor, You Must Add The Code. 31

32 Add Code For The Student Destructor Below main We Already Have The Prototype In The Class Student Each & Every Class Method/Function Will Include The Name Student::~Student(void) { } Of The Class (Student) & The Scope Operator ( :: ) After The Scope Operator ( :: ) Will Be The Name Of The Function Destructors Begin With ~ Inside The Parenthesis Are The Arguments Passed None This Time COMPILE THE PROGRAM! if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Destructor ~Student(void)"); 32

33 int main (int argc, char * argv[]) { Add The Following Class To main COMPILE THE PROGRAM! puts (" "); puts (" "); puts ("++ Class Student Added To Main ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks ++"); puts (" "); puts (" \n"); puts (" Start Of Main \n"); } { // Object John Created In This Scope Student John; } // Object John Will Be Destroyed In This Scope puts (" End Of Main \n"); HitCarriageReturnToContinue(); return (0); 33

34 34

35 About Classes - 1 Each class has a Public Portion containing Methods & Data Available to All that utilize the class. Each class has a Private Portion containing Methods & Data Available Only Available To Member Functions the & Friends Of This Class. The Constructor, whose name is the same as the class, does all that is necessary to Create An Object. 35

36 About Classes - 2 The Destructor, whose name is ~ + the name of the class, does all that is necessary to Destroy An Object. All Classes have a Destructor; if the programmer does not create one, the compiler creates one which does nothing. Class Functions are called Methods or Member Functions [Constructor, Destructor, Set, Display]. A Class Bundles Both Data & Methods [Encapsulation] this was not so with c structs. Class Data called Data Members [ Name, No, Gender]. 36

37 About Classes - 3 Times When You Need To Code The Destructor 1] When Constructor Opens The File, The Destructor Should Generally Close It. 2] When The Constructor Allocates Dynamic Memory, The Destructor Should Generally Return That Memory To The Operating System. 3] When The Constructor Turns On An A to D Card, the Destructor Should Generally Turn It Off. 37

38 Time To Organize Testing! 38

39 # include "Utilities.hpp" // Defines # define MALE false # define FEMALE true # define STUDENT_CLASS # define STUDENT_DIAGNOSTIC_LEVEL 1 // Classes & Structs class Student Objective: { public: Student(void); ~Student(void); private: char Name[20]; long No; bool Gender; }; Add The Following Class To Your Program Create An Organized Testing Procedure That Can Be Quickly & Easily Turned On & Off. We Will Turn It On During Development Then Turn It Off We Will Turn It Back On When Class Is Modified During Maintenance! // Prototypes # ifdef STUDENT_DIAGNOSTIC_LEVEL // void TestStudent(void); # endif // STUDENT_DIAGNOSTIC_LEVEL

40 Modify Constructor Diagnostic Level Student::Student(void) { # ifdef STUDENT_DIAGNOSTIC_LEVEL // if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student(void)"); # endif // STUDENT_DIAGNOSTIC_LEVEL } strcpy_s(name, ""); No = 0; Gender = MALE; When we first create the constructor and destructor, it is a great idea to make sure that they are being executed. # define STUDENT_DIAGNOSTIC_LEVEL 12 Once we are sure they are being executed, we will set the Diagnostic Level to 2 The message "Evoking Constructor Student(void)" would no longer display. 40

41 Suppose We Were To Comment Out The STUDENT_CLASS -1 # include "Utilities.hpp" // Defines # define MALE false # define FEMALE true //# define STUDENT_CLASS # define STUDENT_DIAGNOSTIC_LEVEL 1 // Classes & Structs class Student { If We Were To Comment Out the define for public: STUDENT_CLASS Student(void); ~Student(void); private: char Name[20]; long No; bool Gender; }; I Will Model This With A Lot Of The Test Code That I Give You This Semester! Then The Prototype for TestStudent Would No Longer Be Associated With The Class. IF DONE CORRECTLY We Will Be Able To Leave All Of The Test Code To Reuse When It Is Necessary Modify The Class Later For Maintenance! // Prototypes # ifdef STUDENT_DIAGNOSTIC_LEVEL // void TestStudent(void); # endif // STUDENT_DIAGNOSTIC_LEVEL

42 Suppose We Were To Comment Out The STUDENT_CLASS -2 //# define STUDENT_CLASS Student::Student(void) { # ifdef STUDENT_DIAGNOSTIC_LEVEL // if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student(void)"); # endif // STUDENT_DIAGNOSTIC_LEVEL } strcpy_s(name, ""); No = 0; Gender = MALE; If We Were To Comment Out the define for STUDENT_CLASS Then Diagnostic Level Display Code Would No Longer Be Assocaited With the Class We Will Be Able To Keep All Of Our Test Code And Yet Add 0 Bytes To Our Final Compiled Projects! 2 42

43 Modify Constructor Diagnostic Level //# define STUDENT_CLASS Student::~Student(void) { # ifdef STUDENT_DIAGNOSTIC_LEVEL // if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Destructor ~Student(void)"); # endif // STUDENT_DIAGNOSTIC_LEVEL } If We Were To Comment Out the define for STUDENT_CLASS Then Diagnostic Level Display Code Would No Longer Be Assocaited With the Class 43

44 44

45 Add TestStudent Below main # ifdef STUDENT_DIAGNOSTIC_LEVEL // void TestStudent(void) COMPILE THE PROGRAM! { puts("\n\n"); puts("*******************************************************************"); puts("*******************************************************************"); puts("************************ Start TestStudent ************************"); puts("*******************************************************************"); puts("*******************************************************************"); puts("*******************************************************************"); puts("*******************************************************************"); puts("************************ End TestStudent ************************"); puts("*******************************************************************"); puts("*******************************************************************"); } # endif // STUDENT_DIAGNOSTIC_LEVEL All of out test code will go away when we comment out //# define STUDENT_CLASS 45

46 46

47 Add TestStudent Below main # ifdef STUDENT_DIAGNOSTIC_LEVEL // void TestStudent(void) { puts("\n\n"); puts("*******************************************************************"); puts("*******************************************************************"); puts("************************ Start TestStudent ************************"); puts("*******************************************************************"); puts("*******************************************************************"); puts("*******************************************************************"); puts("*******************************************************************"); puts("************************ End TestStudent ************************"); puts("*******************************************************************"); puts("*******************************************************************"); } # endif // STUDENT_DIAGNOSTIC_LEVEL

48 Add Diagnostic Level 1-A (In TestStudent) if (STUDENT_DIAGNOSTIC_LEVEL <= 1) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Constructor & Destructor =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 1 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student1, Class[4]; } HitCarriageReturnToContinue(); 48

49 int main (int argc, char * argv[]) { puts (" "); puts (" "); puts ("++ Class Student Added To Main ++"); puts ("++ ++"); puts ("++ Writen By ++"); puts ("++ Dr. Tom Hicks ++"); puts (" "); puts (" \n"); puts (" Start Of Main \n"); TestStudent(); Add The Following Class To main COMPILE THE PROGRAM! } puts (" HitCarriageReturnToContinue(); return (0); End Of Main \n"); 49

50 50

51 Modify Diagnostic Level 1-B if (STUDENT_DIAGNOSTIC_LEVEL <= 1) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Constructor & Destructor =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 1 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student1, Student2 ("Pete", 2222, MALE), Class[4]; } HitCarriageReturnToContinue(); Suppose We Would Like Be Able To Create & Initialize A Student Object Our Constructor Only Accepts A Signature Student(-) We Could Create A Constructor To Accept A Signature Student(char[ ], int, bool) 51

52 Student (char[ ], long, bool) 52

53 Add A Prototype For New Constructor To Class Student // Classes & Structs class Student { public: Student(void); Student (char NewName[], long NewNo, bool NewGender); ~Student(void); private: char Name[20]; long No; bool Gender; }; Will Not Work? Student (char Name[], long No, bool Gender); Class Data Member Called Name - Parameter Called Name NO 53

54 Student (char [ ],long, bool) Student::Student(char NewName[], long NewNo, bool NewGender) { # ifdef STUDENT_DIAGNOSTIC_LEVEL // if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student (NewName[], NewNo, NewGender)"); # endif // STUDENT_DIAGNOSTIC_LEVEL } strcpy_s(name, NewName); No = NewNo; Gender = NewGender; COMPILE THE PROGRAM! Constructor Student Is Overloaded! Accepts More Than One Signature! 54

55 55

56 if (STUDENT_DIAGNOSTIC_LEVEL <= 1) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Constructor & Destructor =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 1 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Student1, Student2 ("Pete", 2222, MALE), Student3 ("Sandra", 3333), Class[4]; Modify Diagnostic Level 1-C } HitCarriageReturnToContinue(); Suppose We Would Like Be Able To Accept A Signature Student(char[ ], long) We could create "yet another constructor" but this provides me with the opportunity to discuss "Default Arguments" 56

57 57

58 Default Arguments Must Be Placed Only In Prototype Modify Class Student // Classes & Structs class Student { public: Student(void); Student (char NewName[], long NewNo, bool NewGender = MALE); ~Student(void); private: char Name[20]; long No; bool Gender; }; In The Event That We Choose Not To Pass A Gender It Becomes MALE Requires No Change to the this Constructor Student(char [ ], long, bool) to make it accept Student(char [ ], long) Will Sometimes Compile in linux and other times not! Be Careful! 58

59 This Now Accepts The Gender Is Optional Student (char [ ],long) & Student (char [ ],long, bool) Prototype: Student (char NewName[], long NewNo, bool NewGender = MALE); Student::Student(char NewName[], long NewNo, bool NewGender) { # ifdef STUDENT_DIAGNOSTIC_LEVEL // if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student (NewName[], NewNo, NewGender)"); # endif // STUDENT_DIAGNOSTIC_LEVEL } strcpy_s(name, NewName); No = NewNo; Gender = NewGender; COMPILE THE PROGRAM! There May Be More Than One Default Argument but they must all be in a group going Right To Left. Student (char NewName[], long NewNo, bool NewGender = MALE); OK Student (char NewName[], long NewNo = 0, bool NewGender = MALE); OK Student (char NewName[]="", long NewNo = 0, bool NewGender = MALE); OK 59

60 This Now Accepts Compile Student(-), Student (char [ ],long), & Student (char [ ],long, bool) Prototype: Student(void); Student (char NewName[], long NewNo, bool NewGender = MALE); Student::Student(char NewName[], long NewNo, bool NewGender) { # ifdef STUDENT_DIAGNOSTIC_LEVEL // if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student (NewName[], NewNo, NewGender)"); # endif // STUDENT_DIAGNOSTIC_LEVEL } strcpy_s(name, NewName); No = NewNo; Gender = NewGender; COMPILE THE PROGRAM! 60

61 61

62 Modify Diagnostic Level 1-D Final Testing Must Accept All Of The Following if (STUDENT_DIAGNOSTIC_LEVEL <= 1) { puts("\n\n"); puts("==================================================================="); puts("==================================================================="); puts("=========== Testing Constructor & Destructor =============="); puts("=========== STUDENT_DIAGNOSTIC_LEVEL = 1 =============="); puts("==================================================================="); puts("===================================================================\n"); Student Several Different Solutions Student1, Student2 ("Pete", 2222, MALE), Student3 ("Sandra", 3333), Student4 ("Amber"), Student5 (5555, "Nathan", MALE), Student6 (6666, "Betty"), Student7 (7777), Class[4]; } HitCarriageReturnToContinue(); 62

63 Suppose: Match Function Calls With Constructors 1 public: (1) Student(void); (2) Student (char NewName[], long NewNo, bool NewGender); Student Student1, Student2 ("Pete", 2222, MALE), Student3 ("Sandra", 3333), Student4 ("Amber"), Student5 (5555, "Nathan", MALE), Student6 (6666, "Betty"), Student7 (7777), Class[4]; 63

64 Suppose: Match Function Calls With Constructors 2 public: (1) Student(void); (2) Student (char NewName[], long NewNo = 0, bool NewGender); Student Student1, Student2 ("Pete", 2222, MALE), Student3 ("Sandra", 3333), Student4 ("Amber"), Student5 (5555, "Nathan", MALE), Student6 (6666, "Betty"), Student7 (7777), Class[4]; 64

65 Suppose: Match Function Calls With Constructors 3 public: (1) Student(void); (2) Student (char NewName[] = "", long NewNo = 0, bool NewGender); Student Student1(), Student2 ("Pete", 2222, MALE), Student3 ("Sandra", 3333), Student4 ("Amber"), Student5 (5555, "Nathan", MALE), Student6 (6666, "Betty"), Student7 (7777), Class[4]; The Compiler Does Not Know What To Do Since There Are Two Constructors That Accept Student(-) Not Compile! Constructor (1) Accepts Signatures : Student(-) Constructor(2) Accepts Signatures : Student(-) Student(char [ ]) Student(char [ ], long) Student(char [ ], long, bool) 65

66 Suppose: Match Function Calls With Constructors 4 public: (1) Student (long NewNo,char NewName[] = "", bool NewGender); (2) Student (char NewName[] = "", long NewNo = 0, bool NewGender); Student Student1, Student2 ("Pete", 2222, MALE), Student3 ("Sandra", 3333), Student4 ("Amber"), Student5 (5555, "Nathan", MALE), Student6 (6666, "Betty"), Student7 (7777), Class[4]; Suppose We Get Rid Of Constructor (1) As It Is Was & Replace It No Conflicts We Have A Plan! Constructor(1) Accepts Signatures : Student (long) Student (long, char [ ]) Student (long, char [ ], bool) 66

67 Student (long, char[ ], bool) 67

68 Double-Check Your Prototypes In Class Student // Classes & Structs class Student { public: Student (char NewName[] = "", long NewNo = 0, bool NewGender); Student (long NewNo,char NewName[] = "", bool NewGender = MALE); ~Student(void); private: char Name[20]; long No; bool Gender; }; 68

69 Student (char [ ],long, bool) Student::Student (long NewNo, char NewName[], bool NewGender) { # ifdef STUDENT_DIAGNOSTIC_LEVEL // if (STUDENT_DIAGNOSTIC_LEVEL <= 1) puts("evoking Constructor Student (NewNo, NewName[], NewGender)"); # endif // STUDENT_DIAGNOSTIC_LEVEL } strcpy_s(name, NewName); No = NewNo; Gender = NewGender; COMPILE THE PROGRAM! Class[4] Should Call The Constructor 4 times You Can Look At The Output & Match Up The Function Calls! 69

70 Look At Total Output Note that 11 constructors are fired & 11 destructors are fired! But We Still Don't Know If The Constructors Are Working Correctly! 70

71 71

72 Add The Following Block Of Code Immediately Above Your Constructors ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // Method Student // // // // Purpose : Constructors for Class Student. Set Name to NewName and No to // // NewNo and Gender to NewGender. Default NewName = blank. // // Default NewNo = 0. Default Sex = FEMALE = false. // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// Use Your Name & Today's Date 72

73 My Constructor Code 73

74 74

75 Add The Following Block Of Code Immediately Above Your Destructor ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// // ~Student // // // // Purpose : Do all that is necessary to destroy an object. In this class we // // are only going to insert a diagnostic display to verify the the // // destructor is firing properly. // // // // Written By : Dr. Tom Hicks Environment : Windows 10 // // Date...: xx/xx/xxxx Compiler...: Visual Studio 2017 C++ // ////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////// Use Your Name & Today's Date 75

76 My Destructor Code 76

77 77

78 Always Back Up Your Class Work Make a copy of folder TomH-Student-1 Your Name on your Y Drive. Make a copy of folder TomH-Student-1 Your Name on your personal computer. Make a copy of folder TomH-Student-1 Your Name on your personal flash drive. 78

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!

RECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function! 1 RECOMMENDATION Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function! 2 3 Copy Project Folder There will be a number of times when

More information

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points

OOP- 4 Templates & Memory Management Print Only Pages 1-5 Individual Assignment Answers To Questions 10 Points - Program 15 Points OOP-4-Templates-Memory-Management-HW.docx CSCI 2320 Initials P a g e 1 If this lab is an Individual assignment, you must do all coded programs on your own. You may ask others for help on the language syntax,

More information

C:\Temp\Templates. Download This PDF From The Web Site

C:\Temp\Templates. Download This PDF From The Web Site 11 2 2 2 3 3 3 C:\Temp\Templates Download This PDF From The Web Site 4 5 Use This Main Program Copy-Paste Code From The Next Slide? Compile Program 6 Copy/Paste Main # include "Utilities.hpp" # include

More information

# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate!

# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate! 1 2 Objectives 1. Agregates 2. Structs 3. Introduction To Classes 4. Constructor 5. Destructor 6. Function Overloading 7. Default Arguments 8. Accessors & Mutators 9. ADT Abstract Data Types 10. Operator

More information

2 2

2 2 1 2 2 3 3 C:\Temp\Templates 4 5 Use This Main Program 6 # include "Utilities.hpp" # include "Student.hpp" Copy/Paste Main void MySwap (int Value1, int Value2); int main(int argc, char * argv[]) { int A

More information

Call The Project Dynamic-Memory

Call The Project Dynamic-Memory 1 2 2 Call The Project Dynamic-Memory 4 4 Copy-Paste Main # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo) = %d\n", (*PtrNo)); } getchar();

More information

Functions & Memory Maps Review C Programming Language

Functions & Memory Maps Review C Programming Language Functions & Memory Maps Review C Programming Language Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department Constants c 2 What Is A Constant? Constant a Value that cannot be altered by

More information

Final CSE 131B Spring 2004

Final CSE 131B Spring 2004 Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers

Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department c http://carme.cs.trinity.edu/ thicks/2320/schedule.html http://carme.cs.trinity.edu/thicks/2320/schedule.html

More information

OOP- 5 Stacks Individual Assignment 35 Points

OOP- 5 Stacks Individual Assignment 35 Points OOP-5-Stacks-HW.docx CSCI 2320 Initials P a g e 1 If this lab is an Individual assignment, you must do all coded programs on your own. You may ask others for help on the language syntax, but you must organize

More information

CS-201 Introduction to Programming with Java

CS-201 Introduction to Programming with Java CS-201 Introduction to Programming with Java California State University, Los Angeles Computer Science Department Lecture X: Methods II Passing Arguments Passing Arguments methods can accept outside information

More information

AIMS Embedded Systems Programming MT 2017

AIMS Embedded Systems Programming MT 2017 AIMS Embedded Systems Programming MT 2017 Object-Oriented Programming with C++ Daniel Kroening University of Oxford, Computer Science Department Version 1.0, 2014 Outline Classes and Objects Constructors

More information

Common Misunderstandings from Exam 1 Material

Common Misunderstandings from Exam 1 Material Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself

More information

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011 More C++ David Chisnall March 17, 2011 Exceptions A more fashionable goto Provides a second way of sending an error condition up the stack until it can be handled Lets intervening stack frames ignore errors

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Supporting Class / C++ Lecture Notes

Supporting Class / C++ Lecture Notes Goal Supporting Class / C++ Lecture Notes You started with an understanding of how to write Java programs. This course is about explaining the path from Java to executing programs. We proceeded in a mostly

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

eingebetteter Systeme

eingebetteter Systeme Praktikum: Entwicklung interaktiver eingebetteter Systeme C++-Tutorial (falk@cs.fau.de) 1 Agenda Classes Pointers and References Functions and Methods Function and Operator Overloading Template Classes

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

OOP-8-DLList-1-HW.docx CSCI 2320 Initials Page 1

OOP-8-DLList-1-HW.docx CSCI 2320 Initials Page 1 OOP-8-DLList-1-HW.docx CSCI 2320 Initials Page 1 If this lab is an Individual assignment, you must do all coded programs on your own. You may ask others for help on the language syntax, but you must organize

More information

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level

More information

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions C++: Const Function Overloading Constructors and Destructors Enumerations Assertions Const const float pi=3.14159; const int* pheight; // defines pointer to // constant int value cannot be changed // pointer

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each). A506 / C201 Computer Programming II Placement Exam Sample Questions For each of the following, choose the most appropriate answer (2pts each). 1. Which of the following functions is causing a temporary

More information

COMP 2355 Introduction to Systems Programming

COMP 2355 Introduction to Systems Programming COMP 2355 Introduction to Systems Programming Christian Grothoff christian@grothoff.org http://grothoff.org/christian/ 1 Today Class syntax, Constructors, Destructors Static methods Inheritance, Abstract

More information

Lab 1: First Steps in C++ - Eclipse

Lab 1: First Steps in C++ - Eclipse Lab 1: First Steps in C++ - Eclipse Step Zero: Select workspace 1. Upon launching eclipse, we are ask to chose a workspace: 2. We select a new workspace directory (e.g., C:\Courses ): 3. We accept the

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

C++ Constructor Insanity

C++ Constructor Insanity C++ Constructor Insanity CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Chapter 4C Homework Functions III Individual Assignment 30 Points Questions 6 Points Script 24 Points

Chapter 4C Homework Functions III Individual Assignment 30 Points Questions 6 Points Script 24 Points PCS1-Ch-4C-Functions-3-HW.docx CSCI 1320 Initials P a g e 1 If this lab is an Individual assignment, you must do all coded programs on your own. You may ask others for help on the language syntax, but

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

And Even More and More C++ Fundamentals of Computer Science

And Even More and More C++ Fundamentals of Computer Science And Even More and More C++ Fundamentals of Computer Science Outline C++ Classes Special Members Friendship Classes are an expanded version of data structures (structs) Like structs, the hold data members

More information

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay Computer Science and Engineering Indian Institue of Technology Bombay November 27, 2004 What is Object Oriented Programming? Identifying objects and assigning responsibilities to these objects. Objects

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

C++ 8. Constructors and Destructors

C++ 8. Constructors and Destructors 8. Constructors and Destructors C++ 1. When an instance of a class comes into scope, the function that executed is. a) Destructors b) Constructors c) Inline d) Friend 2. When a class object goes out of

More information

Object Oriented Programming. Solved MCQs - Part 2

Object Oriented Programming. Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 Object Oriented Programming Solved MCQs - Part 2 It is possible to declare as a friend A member function A global function A class All of the above What

More information

Install Visual Studio Community Version 2017

Install Visual Studio Community Version 2017 Dr. Tom Hicks Install Visual Studio Community Version 2017 1 P a g e Install Visual Studio Community Version 2017 1] Navigate your browser to https://www.visualstudio.com/ 2] Hold down the Download Visual

More information

C++ Important Questions with Answers

C++ Important Questions with Answers 1. Name the operators that cannot be overloaded. sizeof,.,.*,.->, ::,? 2. What is inheritance? Inheritance is property such that a parent (or super) class passes the characteristics of itself to children

More information

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm

Cmpt 135 Assignment 2: Solutions and Marking Rubric Feb 22 nd 2016 Due: Mar 4th 11:59pm Assignment 2 Solutions This document contains solutions to assignment 2. It is also the Marking Rubric for Assignment 2 used by the TA as a guideline. The TA also uses his own judgment and discretion during

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

JAVA GUI PROGRAMMING REVISION TOUR III

JAVA GUI PROGRAMMING REVISION TOUR III 1. In java, methods reside in. (a) Function (b) Library (c) Classes (d) Object JAVA GUI PROGRAMMING REVISION TOUR III 2. The number and type of arguments of a method are known as. (a) Parameter list (b)

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 5 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 13, 2013 Question 1 Consider the following Java definition of a mutable string class. class MutableString private char[] chars

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor

More information

Department of Computer science and Engineering Sub. Name: Object oriented programming and data structures Sub. Code: EC6301 Sem/Class: III/II-ECE Staff name: M.Kavipriya Two Mark Questions UNIT-1 1. List

More information

Today we spend some time in OO Programming (Object Oriented). Hope you did already work with the first Starter and the box at:

Today we spend some time in OO Programming (Object Oriented). Hope you did already work with the first Starter and the box at: maxbox Starter 2 Start with OO Programming 1.1 First Step Today we spend some time in OO Programming (Object Oriented). Hope you did already work with the first Starter and the box at: http://www.softwareschule.ch/download/maxbox_starter.pdf

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 4 : Classes & Objects Lecture Contents What is a class? Class definition: Data Methods Constructors Properties (set/get) objects

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

Advanced Systems Programming

Advanced Systems Programming Advanced Systems Programming Introduction to C++ Martin Küttler September 19, 2017 1 / 18 About this presentation This presentation is not about learning programming or every C++ feature. It is a short

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 4 (a): Classes & Objects Lecture Contents 2 What is a class? Class definition: Data Methods Constructors objects Static members

More information

Object Oriented Programming in C#

Object Oriented Programming in C# Introduction to Object Oriented Programming in C# Class and Object 1 You will be able to: Objectives 1. Write a simple class definition in C#. 2. Control access to the methods and data in a class. 3. Create

More information

Final CSE 131B Spring 2005

Final CSE 131B Spring 2005 Login name Signature Name Student ID Final CSE 131B Spring 2005 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (27 points) (24 points) (32 points) (24 points) (32 points) (26 points) (31 points)

More information

Lab 4: Tracery Recursion in C with Linked Lists

Lab 4: Tracery Recursion in C with Linked Lists Lab 4: Tracery Recursion in C with Linked Lists For this lab we will be building on our previous lab at the end of the previous lab you should have had: #include #include char * make_string_from

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming s SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177 Programming Time allowed: THREE hours: Answer: ALL questions Items permitted: Items supplied: There is

More information

Topic 10: Introduction to OO analysis and design

Topic 10: Introduction to OO analysis and design Topic 10: Introduction to OO analysis and design Learning Outcomes Upon successful completion of this topic you will be able to: design classes define class member variables and functions explain public

More information

Chapter 10 Introduction to Classes

Chapter 10 Introduction to Classes C++ for Engineers and Scientists Third Edition Chapter 10 Introduction to Classes CSc 10200! Introduction to Computing Lecture 20-21 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this

More information

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016 CS 31: Intro to Systems Pointers and Memory Martin Gagne Swarthmore College February 16, 2016 So we declared a pointer How do we make it point to something? 1. Assign it the address of an existing variable

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

More information

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3 Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1 Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components

More information

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck C++ Crash Kurs Polymorphism Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck http://www.itm.uni-luebeck.de/people/pfisterer C++ Polymorphism Major abstractions of C++ Data abstraction

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 4 : Classes & Objects Lecture Contents What is a class? Class definition: Data Methods Constructors Properties (set/get) objects

More information

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE? 1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lecture 8: Object-Oriented Programming (OOP) 1 Introduction to C++ 2 Overview Additional features compared to C: Object-oriented programming (OOP) Generic programming (template) Many other small changes

More information

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

G52CPP C++ Programming Lecture 8. Dr Jason Atkin G52CPP C++ Programming Lecture 8 Dr Jason Atkin 1 Last lecture Dynamic memory allocation Memory re-allocation to grow arrays Linked lists Use -> rather than. pcurrent = pcurrent -> pnext; 2 Aside: do not

More information

CS 103 Unit 10 Slides

CS 103 Unit 10 Slides 1 CS 103 Unit 10 Slides C++ Classes Mark Redekopp 2 Object-Oriented Programming Model the application/software as a set of objects that interact with each other Objects fuse data (i.e. variables) and functions

More information

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING

FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING FINAL TERM EXAMINATION SPRING 2010 CS304- OBJECT ORIENTED PROGRAMMING Question No: 1 ( Marks: 1 ) - Please choose one Classes like TwoDimensionalShape and ThreeDimensionalShape would normally be concrete,

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

COSC 2P95 Lab 5 Object Orientation

COSC 2P95 Lab 5 Object Orientation COSC 2P95 Lab 5 Object Orientation For this lab, we'll be looking at Object Orientation in C++. This is just a cursory introduction; it's assumed that you both understand the lecture examples, and will

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

An Introduction to C++

An Introduction to C++ An Introduction to C++ Introduction to C++ C++ classes C++ class details To create a complex type in C In the.h file Define structs to store data Declare function prototypes The.h file serves as the interface

More information

For Teacher's Use Only Q No Total Q No Q No

For Teacher's Use Only Q No Total Q No Q No Student Info Student ID: Center: Exam Date: FINALTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Time: 90 min Marks: 58 For Teacher's Use Only Q No. 1 2 3 4 5 6 7 8 Total Marks Q No. 9

More information

Data Structures (list, dictionary, tuples, sets, strings)

Data Structures (list, dictionary, tuples, sets, strings) Data Structures (list, dictionary, tuples, sets, strings) Lists are enclosed in brackets: l = [1, 2, "a"] (access by index, is mutable sequence) Tuples are enclosed in parentheses: t = (1, 2, "a") (access

More information

Classes. Christian Schumacher, Info1 D-MAVT 2013

Classes. Christian Schumacher, Info1 D-MAVT 2013 Classes Christian Schumacher, chschuma@inf.ethz.ch Info1 D-MAVT 2013 Object-Oriented Programming Defining and using classes Constructors & destructors Operators friend, this, const Example Student management

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 FUNCTIONS INTRODUCTION AND MAIN All the instructions of a C program are contained in functions. üc is a procedural language üeach function performs a certain task A special

More information

Introduction to Classes

Introduction to Classes Introduction to Classes Procedural and Object-Oriented Programming Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented

More information

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2 CMPT 125 Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance John Edgar 2 Edit or write your program Using a text editor like gedit Save program with

More information

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C. Reading. goto Considered Harmful History Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program

More information

CS-220 Spring 2018 Test 1 Version A Feb. 28, Name:

CS-220 Spring 2018 Test 1 Version A Feb. 28, Name: CS-220 Spring 2018 Test 1 Version A Feb. 28, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : Every function definition in C must

More information

CS 103 Unit 10 Slides

CS 103 Unit 10 Slides 1 CS 103 Unit 10 Slides C++ Classes Mark Redekopp 2 Object-Oriented Programming Model the application/software as a set of objects that interact with each other Objects fuse data (i.e. variables) and functions

More information

Interview Questions of C++

Interview Questions of C++ Interview Questions of C++ Q-1 What is the full form of OOPS? Ans: Object Oriented Programming System. Q-2 What is a class? Ans: Class is a blue print which reflects the entities attributes and actions.

More information

class Array; // Class template declaration class Array public: // T="type",e.g.,int or float Array(int n); // Create array of n elements Array(); // C

class Array; // Class template declaration class Array public: // T=type,e.g.,int or float Array(int n); // Create array of n elements Array(); // C LECTURE 5 Templates We have written a simple array class of oat variables. But suppose we want to have arrays of integers, or doubles, or something else. It's a pain to write a separate array class for

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 8 C: Miscellanea Control, Declarations, Preprocessor, printf/scanf 1 The story so far The low-level execution model of a process (one

More information

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-43 Exam Review February, 01 Presented by the RIT Computer Science Community http://csc.cs.rit.edu C Preprocessor 1. Consider the following program: 1 # include 3 # ifdef WINDOWS 4 # include

More information

Chapter 2. Procedural Programming

Chapter 2. Procedural Programming Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic

More information