140 likes | 152 Views
Learn about class member variables, functions, constructors, and abstract data types in a simple system. Understand the syntax for defining and calling constructors, along with public/private member variables and functions. Practice initializing objects and calling constructors in C++ software engineering.
E N D
Lecture 8: A Simple System • Software Engineering: • Class • member variables/functions • constructor (function) • A simple system -elevator CS3369 Realtime Control Computer Software/DENG Xiaotie
Classes and Abstract Data Types(ADT) • Structures: A data type with many member variables • Classes: A data type with many member variables and member functions. • ADT: Data types users know functionality of public member variables and member functions but not necessary the implementation details. CS3369 Realtime Control Computer Software/DENG Xiaotie
Class • A class is similar to a data type and an object in a class is similar to a variable in a data type. • A class may have • associated member variables which is accessed via a dot after the name of the object and the variable name after the dot. • Example: obj1.x1 or obj1.array[6] • associated member functions which is accessed via a dot after the name of the object and the function name after the dot and parameter list in parenthesis after the function name. • Example: obj1.f(x1,x2) CS3369 Realtime Control Computer Software/DENG Xiaotie
public member variables/functions: can be accessed in any function • private member variables/functions: can only be accessed in member functions of the same class. CS3369 Realtime Control Computer Software/DENG Xiaotie
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; CS3369 Realtime Control Computer Software/DENG Xiaotie
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; CS3369 Realtime Control Computer Software/DENG Xiaotie
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 } CS3369 Realtime Control Computer Software/DENG Xiaotie
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} CS3369 Realtime Control Computer Software/DENG Xiaotie
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; CS3369 Realtime Control Computer Software/DENG Xiaotie
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(); CS3369 Realtime Control Computer Software/DENG Xiaotie
#include <iostream.h> • #include <student.h> • main(){StudentRecord x(971423,80,80,60);//constructor is • // called according 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; • } CS3369 Realtime Control Computer Software/DENG Xiaotie
#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;} CS3369 Realtime Control Computer Software/DENG Xiaotie
A simple System-an elevator system • Define a class for an elevator • array[6]- for the 6 floors of the building, if a person press ‘i’ in the elevator, then array[I-1] is set to be 1. • sign-indicates the elevator is going “up” or “down”. • k- the present position of the elevator. • count-the number of stops in this round. CS3369 Realtime Control Computer Software/DENG Xiaotie
A simple System-an elevator system(continued) • Description of the system: • there are two (or three) elevators to serve the building • Control strategies: • when an elevator reaches a level, it will test if stop is required and will stop if yes. • To balance the number of people/number of stops, we use count to remember the number of times it stopped in this round (from 1 to 6 or from 6 to 1). CS3369 Realtime Control Computer Software/DENG Xiaotie