Download presentation
Presentation is loading. Please wait.
1
CS5253 Workshop I Lecturer: Dr. Lusheng Wang
Office: Y6429, Academic Building Phone: Homepage:
2
Course Information Objective: Assessment:
To get familiar with object oriented design and gain solid experience in C++ programming. Assessment: Assignments 100% Three assignments
3
Programming Language Assembly Language High-level Language
e.g. FORTRAN, BASIC Structured Programming e.g. Pascal, C Object-oriented Programming e.g. C++, java
4
Object Oriented Programming
3 Important Concepts: Encapsulation Polymorphism Inheritance
5
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”.
6
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
7
Polymorphism Polymorphism: Two forms: One name, many purposes
Function overloading Operator overloading
8
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().
9
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.
10
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.
11
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; }
12
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.
13
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.
14
A Simple Example (cont’d)
Define Member Functions: void my_class::set_a(int num) { a = num; } int my_class::get_a() { return a;
15
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; }
16
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(); };
17
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;
18
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
19
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
20
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
21
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
22
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
23
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
24
#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
25
#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
26
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}; };
27
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}; };
28
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.
29
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
30
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];
31
Delete Dynamically Allocated Arrays
To delete a dynamically allocated array: delete [] p-var; Example: delete [] p;
32
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.
33
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; }
34
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
35
Example: object pointers (consider my_class again)
class my_class { int a; public: void set_a(int num); int get_a(); };
36
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();
37
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; }
38
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;
39
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.