1 / 14

Understanding Classes and Objects in Software Engineering

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.

norrisc
Download Presentation

Understanding Classes and Objects in Software Engineering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 8: A Simple System • Software Engineering: • Class • member variables/functions • constructor (function) • A simple system -elevator CS3369 Realtime Control Computer Software/DENG Xiaotie

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. #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

  12. #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

  13. 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

  14. 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

More Related