390 likes | 402 Views
Gain solid experience in C++ programming and become familiar with object-oriented design principles. Assessments are done through programming assignments.
E N D
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: • 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: • 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: • One name, many purposes • Two forms: • 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: • 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: classclass-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 • 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 • 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;
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;
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 }
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}
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;
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();
#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; • }
#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;}
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 top B A C B A D C B A E D C B A D C B A top top top top A top Pushing and poping elements in a stack