CS5253 Workshop I Lecturer: Dr. Lusheng Wang

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
C++ Classes & Data Abstraction
EEM 480 Algorithms and Complexity by Assist. Prof. Dr. Emin Germen.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
C++ fundamentals.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 2 Objects and Classes
Constructors and Destructors
Procedural and Object-Oriented Programming
Functions + Overloading + Scope
Pointers and Dynamic Arrays
EGR 2261 Unit 11 Pointers and Dynamic Variables
Topic: Classes and Objects
Classes C++ representation of an object
Class and Objects UNIT II.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
CISC181 Introduction to Computer Science Dr
Review: Two Programming Paradigms
C++, OBJECT ORIENTED PROGRAMMING
Chapter 5 Classes.
Pointers Revisited What is variable address, name, value?
Finally! Discussing a language!
Chapter 2 Objects and Classes
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
void Pointers Lesson xx
Lecture 9 Concepts of Programming Languages
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Introduction to Classes
Popping Items Off a Stack Lesson xx
Review for Final Exam.
Lecture 8: A Simple System
Dr. Bhargavi Dept of CS CHRIST
Constructors and Destructors
CPS120: Introduction to Computer Science
Review for Final Exam.
Introduction to Data Structure
Java Programming Language
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes C++ representation of an object
CPS120: Introduction to Computer Science
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Pointers and References
C++ Object Oriented 1.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

CS5253 Workshop I Lecturer: Dr. Lusheng Wang Office: Y6429, Academic Building Phone: 2788-9820 email: cswangl@cs.cityu.edu.hk Homepage: www.cs.cityu.edu.hk/~lwang/

Course Information Objective: Assessment: To get familiar with object oriented design and gain solid experience in C++ programming. Assessment: Assignments 100% Three assignments

Programming Language Assembly Language High-level Language e.g. FORTRAN, BASIC Structured Programming e.g. Pascal, C Object-oriented Programming e.g. C++, java

Object Oriented Programming 3 Important Concepts: Encapsulation Polymorphism Inheritance

Encapsulation Encapsulation: Object: a mechanism that binds together code and data it manipulates Object: the device that supports encapsulation code and data are linked together, works like a “black box”.

Encapsulation Within an object, code, data or both can be private to that object or public. Private accessible only by another part of the object. Public accessible by all others

Polymorphism Polymorphism: Two forms: One name, many purposes Function overloading Operator overloading

Polymorphism Function Overloading: In C, three functions for the absolute value action: abs(), labs() and fabs() In C++, each function can be called by the same name, such as abs().

Polymorphism Operator Overloading In C, the ‘+’ operator is used to add integers, long integers, characters, and floating-point values. In C++, you can extend this concept to other data types. e.g. use ‘+’ to add two matrices.

Inheritance Inheritance: Base Class: Derived Class: the process by which one object can acquire the properties of another. Base Class: defines all qualities that will be common to any derived classes. Derived Class: inherits properties of base class and adds properties that are specific to that class.

C++ Console I/O #include <iostream.h> int main(){ int i; cout << "Enter an integer: "; cin >> i; cout << "Here's your number: " << i << "\n"; return 0; }

Classes: A First Look Syntax of a class declaration: class class-name { // private functions and variables public: // public functions and variables } object-list Functions and variables declared within a class are said to be members of that class.

A Simple Example class my_class { Declaration of a new class called my_class. class my_class { int a; // private variable public: void set_a(int num); int get_a(); }; set_a() and get_a() are called member functions.

A Simple Example (cont’d) Define Member Functions: void my_class::set_a(int num) { a = num; } int my_class::get_a() { return a;

A Simple Example (cont’d) Demonstrate the user of my_class: int main() { my_class ob1; ob1.set_a(10); cout << ob1.get_a() << “\n”; return 0; }

Constructor Function class my_class { Assume you want to initialize the value of a each time an object of my_class is created. class my_class { int a; public: my_class(int x); // constructor void set_a(int num); int get_a(); };

Constructor Function void my_class::my_class(int x) { a = x; } int main() { // give an initial value of 10 my_class ob2(10); cout << ob2.get_a() << “\n” return 0;

Syntax for Prototype a Class class class_name{//prototype a class public: //public functions and variables double a_function(); //prototype a member function int i; //prototype an int private: //private functions and variables no other //functions can access it except of the same class void auxi_function(); //an auxiliary function long another_variable; //private int }; //end definition with semicolon; 11/23/2018

class StudentRecord{//example of prototype class public: //so that other functions can access it. double final(); //prototype of member function void print_out(); //prototype output member function void get_scores();//prototype input member function private: //no other functions can access //it except in the same class long int ID; //student id is long integer int score1, score2, exam; //scores of hmwk 1,2, exam double w1=0.25, w2=0.25, w3=0.5; }; //end definition with semicolon; 11/23/2018

Syntax for define member functions Return_type class_name:: function_name() { //statements of what to do with the function; //member variables and functions may be used //additional auxiliary variables may also be defined //and used } 11/23/2018

void StudentRecord:: get_scores(){ cout << “enter student ID” <<endl; cin >> ID; //use of member variable cout << “enter his scores in hmwk1, hmwk2, exam\n”; cin >> score1>>score2>>exam; //use of member variables } void StudentRecord:: print_out() { cout << “Final score of ”<<ID<<“ is ”<<final()<<endl; //member variable is used. double StudentRecord::final() {//member function definition return (w1*score1+w2*score2+w3*exam); //member variables are used} 11/23/2018

Initialization with constructors: class StudentRecord{ public: //so that other functions can access it. double final();//prototype of member function void print_out(); //prototype output member function void get_scores(); //prototype input member function StudentRecord(long id,int mark1, int mark2, int mark3); //a constructor prototype; private: long int ID; //student id is long integer int quiz, lab_work, exam; //marks for them double w1=0.2, w2=0.2, w3=0.6; }; //end definition with semicolon; 11/23/2018

Calling a constructor: class_name Obj_name(arguments for constructor); Object=class_name(arguments for constructor); Default constructor: class_name(); //definition Calling default constructor: class_name Obj_name; Object=class_name(); 11/23/2018

#include <iostream.h> #include <student.h> main(){StudentRecord x(971423,80,80,60);//constructor is // called according to the following definition. x.print_out(); return 0;} StudentRecord::StudentRecord(long id,int mark1, int mark2, int mark3) {ID=id; quiz=mark1; lab_work=mark2; exam=mark3; } 11/23/2018

#include <iostream.h> #include <student.h> char anymore(); //prototype main(){char more; StudentRecord x; do { x.get_scores(); //can be accessed in main() since it is //a public function x.print_out(); //the same reason as above more=anymore(); } while (more= =‘Y’); return 0;} 11/23/2018

Memory allocation: new class my_class2 { int *b; public: my_class2(int x) {b = new int; *b = x;} void set_b(int num) {*b = num;} int get_b() {return *b}; };

Destructor Function, delete class my_class2 { int *b; public: my_class2(int x) {b = new int; *b = x;} ~my_class2() {delete b;} void set_b(int num) {*b = num;} int get_b() {return *b}; };

Example using my_class2 int main() { my_class2 ob2(10); cout << ob2.get_b() << "\n"; return 0; } my_class2 behaves exactly the same as my_class, though their internal structure is different.

Initializing Dynamically Allocated Variables Dynamically allocated variables can be given initial values: p-var = new type (initial-value) Example: int *p; p = new int(9); //give initial value of 9

Create Dynamically Allocated Arrays To dynamically allocated a 1-dim array: p-var = new type [size]; Example: int *p; // allocate room for 5 integers p = new int [5];

Delete Dynamically Allocated Arrays To delete a dynamically allocated array: delete [] p-var; Example: delete [] p;

Declarations of Local Variables In C, local variables can be declared only at the start of a block. In C++, local variables can be declared anywhere. Advantage: Local variables can be declared close to where they are first used, thus helping to prevent unwanted side effects.

Example int main() { int i; cout << “Enter number: ”; cin >> i; int j, fact =1; // variables declared here for (j=i; j>=1; j++) fact = fact * j; cout << “Factorial is” << fact; return 1; }

Object Pointers An object pointer can be declared in the same way as a pointer to other types of variables. int *p1 // pointer to an integer myclass *p2 // pointer to an object // called myclass

Example: object pointers (consider my_class again) class my_class { int a; public: void set_a(int num); int get_a(); };

How to access member functions? Object vs. Object Pointer my_class ob; my_class *p; Dot Operator vs. Arrow Operator ob.get_a(); p->get_a();

Example: object pointers int main(){ my_class ob(120); // create object my_class *p; // create pointer p = &ob; // put address of ob into p cout << ob.get_a(); cout << p->get_a(); return 0; }

Assigning Objects When one object is assigned to another, a bitwise copy of all the data members is made. Example: my_class ob1, ob2; ob2 = ob1;

Stack: A Last-in-First-out List B A C B A D C B A E D C B A top D C B A top top top top A top Pushing and poping elements in a stack