ECE 449 OOP and Computer Simulation Lecture 07 Class Design for Circuit Netlist

Size: px
Start display at page:

Download "ECE 449 OOP and Computer Simulation Lecture 07 Class Design for Circuit Netlist"

Transcription

1 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 1/64 ECE 449 OOP and Computer Simulation Lecture 07 Class Design for Circuit Netlist Professor Jia Wang Department of Electrical and Computer Engineering Illinois Institute of Technology October 6, 2017

2 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 2/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

3 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 3/64 Reading Assignment This lecture: Project 3 Specification Next lecture: 4.1, 11

4 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 4/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

5 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 5/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

6 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 6/64 Building Building: create an executable from a set of source files What is the building process for C++ programs? Why is it designed that way? How should we leverage it to improve productivity? Reuse existing codes/libraries save coding time! Catch typos and mistakes save debugging time!

7 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 7/64 Compiler and Compiling Compiling is the process to turn source codes into binary forms. Many typos and mistakes can be identified by compiler during compiling. The compiler will refuse to generate binary codes until you correct all compiling errors. Example 1: undeclared names Is there a typo in your variable or function name? Are you trying to access a variable or function out of its scope? Example 2: unmatched types Are you assigning a wrong value to a variable? Are the arguments to a function passed in wrong order?

8 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 8/64 Correcting Compiling Errors Don t get frustrated if your code won t compile successfully. Always start with the first error message the remaining ones may simply go away if the first one is corrected. Repeat when necessary until all errors are corrected Read the error message carefully to determine what s wrong. An error message usually starts with the file name and line # that you can locate the line causing the error IDEs may even bring you to the line directly if you double click the error message. Try to follow the description of the error thereafter Only a few types of error messages appear frequently and you will quickly get familiar with them as you code your projects.

9 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 9/64 Separate Compilation So far we have a single C++ file which will be compiled into an executable using the compiler. Even with functions and data types, issues arise when the program becomes more complicated. There are still chances some code is changed accidentally. It takes longer to compile a larger file. Separate compilation: use multiple files for a program Reduce complexity by separating interfaces and implementations Reduce build time by compiling modified files only Reduce chances of accidental changes by copying files instead of code snippets

10 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 10/64 Building, Compiling, and Linking Separate compilation consists of two stages. Usually the whole process is known as building. Stage 1: compiling (by compilers) Convert each source file into an object file (the word object here has nothing to do with OOP) Source files may be written in different programming languages as long as the interfaces are compatible. Stage 2: linking (by a linker) Combine object files into one executable Pre-compiled object files can be provided by a third-party who otherwise won t provide source files. Pre-compiled object files are usually organized into library files.

11 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 11/64 Build Management Compiling For each source file, determine the compiler to compile it To save compiling time, only modified source files should be compiled for each successive build. Determine compiling options Linking Locate the necessary library files Determine the linking options At different stages in software development, the executable may need to be built in different ways. For debugging: comprehensive diagnosis information For final product release: optimize for performance IDEs usually provide automatic build management. Otherwise, a build management tool, e.g. GNU Make, is required. For simplicity, on our CI system, we build with a single command for final product release.

12 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 12/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

13 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 13/64 Function Interface and Implementation bool extract_tokens_from_file(std::string file_name, evl_tokens &tokens) {... // implementation omitted Interface: inputs and outputs of the function, i.e. parameter and return types Implementation: the method to generate outputs from given inputs, i.e. function body Separate compilation for C++ The compiler only need to know the interfaces The linker will locate the implementations

14 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 14/64 Function Declaration // main.cpp bool extract_tokens_from_file(std::string file_name, evl_tokens &tokens); int main(int argc, char *argv[]) {... // verify that argv[1] exists evl_tokens tokens; if (!extract_tokens_from_file(argv[1], tokens)) { return -1;... // display tokens and store them to file Function declaration Define the interface to the function for the compiler Function header followed by ;

15 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 15/64 Function Definition // tokens.cpp bool extract_tokens_from_file(std::string file_name, evl_tokens &tokens) { std::ifstream input_file(file_name.c_str());... std::string line; for (int line_no = 1; std::getline(input_file, line); ++line_no) {... return true; Function definition Function header + function body Define the implementation of the function There should be exactly one definition for each function among all the source files.

16 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 16/64 Issues with Separate Compilation Each source file should contain the declarations for the functions it intends to use. Plus user-defined data types used in those declarations It is tedious to maintain their consistency among multiple source files. Any mismatch in function declaration and function definition will result in linking errors. The linker won t find the desired implementation. Any mismatch of a user-defined data type in multiple source files will result in undefined behavior.

17 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 17/64 Header Files // tokens.h struct evl_token {... ; // struct evl_token typedef std::list<evl_token> evl_tokens; bool extract_tokens_from_file(std::string file_name, evl_tokens &tokens); The most straightforward way to save typing and to maintain consistency is to organize interfaces into header files and to allow source files to access them. Then you will never have mismatched data types. Mismatched function declaration and definition will be caught by linker and is not a concern.

18 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 18/64 Translation Unit // main.cpp #include <vector> #include <iostream> #include <fstream> #include "tokens.h" int main(int argc, char *argv[]) {... // verify that argv[1] exists evl_tokens tokens; if (!extract_tokens_from_file(argv[1], tokens)) { return -1;... // display tokens and store them to file Use #include with "" to include your header files You can include header files from other header files. The source file, together with all the header files it includes directly or indirectly (through other header files), is called a translation unit.

19 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 19/64 #include Guard #ifndef GUARD_TOKENS_H #define GUARD_TOKENS_H struct evl_token {... ; // struct evl_token typedef std::list<evl_token> evl_tokens; bool extract_tokens_from_file(std::string file_name, evl_tokens &tokens); #endif A header file may appear more than once in a translation unit, resulting in compiling errors, e.g., A data type from the header file is defined more than once. There may exist cyclic dependences among header files. Use a #include guard to prevent a header to appear more than once in a translation unit. The header is still included multiple times, though it is only seen by the compiler for the first time.

20 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 20/64 Working with Classes date.h // date.h #ifndef GUARD_DATE_H #define GUARD_DATE_H class date { int year, month, day; public: date(int y, int m, int d); void display(std::ostream &out) const; bool set(int y, int m, int d); ; // class date #endif Don t forget the ; at the end of the class definition. If you see strange compiling error messages, double check all your class definitions for the ;.

21 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 21/64 Working with Classes date.cpp // date.cpp #include <iostream> #include "date.h" date::date(int y, int m, int d) : year(y), month(m), day(d) { void date::display(std::ostream &out) const { out << year << "/" << month << "/" << day << std::endl; bool date::set(int y, int m, int d) {...

22 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 22/64 Working with Classes other.cpp // other.cpp - any other cpp file #include <iostream> #include "date.h" void some_function() { date someday(2017, 10, 8);...

23 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 23/64 Managing Your C++ Files All your source files (.cpp) and header files (.h) should be stored in src. Otherwise, the compiler may fail to find some of your files when building your project. Unused.cpp and.h files should be removed. Don t forget to add all your source files and header files to your repository. Otherwise, the linker may fail to locate some of your function/class implementations. While you won t need separate compilation for Project 2, we believe you need it to manage the complexity of the projects thereafter.

24 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 24/64 Resolving Common Linking Errors Multiple definitions of a function, e.g. main. There should be exactly one definition for each function among all the source files. Did you put a function definition instead of its declaration into a header file? Missing definition of a function. Make sure the parameters of the function declaration matches its definition. Make sure you have git add the.cpp file containing the function definition. Multiple definitions of a class/struct. Don t put a class/struct definition into a.cpp file. Did you use #include guard for all your header files? If you still have troubles, talk to us immediately. Don t waste you time it may take you days to figure out some trivial mistakes when setting up separate compilation.

25 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 25/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

26 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 26/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

27 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 27/64 A 4-Bit Counter module counter4; wire s0, s1, s2, s3, n0, n1, n2, n3, c0, c1, c2, c3; // input/output evl_one(c0); evl_output O(s3, s2, s1, s0); // states wire clk; evl_clock(clk); evl_dff F0(s0, n0, clk); evl_dff F1(s1, n1, clk); evl_dff F2(s2, n2, clk); evl_dff F3(s3, n3, clk); // compute n=s+1 xor X0(n0, s0, c0); xor X1(n1, s1, c1); xor X2(n2, s2, c2); xor X3(n3, s3, c3); and A0(c1, s0, c0); and A1(c2, s1, c1); and A2(c3, s2, c2); endmodule // counter4

28 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 28/64 The Schematic 13 wires, 14 components (clk/evl_clock are not shown) From Project 1 and 2, we know how to obtain them individually in our program. Need to be organized in our C++ program. For simulation, etc.

29 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 29/64 Logic Simulation and Circuit Netlist Programs=Data Structures+Algorithms They are the major contributors to the performance. How to organize them, on the other hand, affects everything else. Logic simulation basics A gate computes output logic signal(s) based on input logic signals according to its pre-defined functionality. A net passes a bit of logic signal from the output of a gate to inputs of other gates. The logic simulation algorithm coordinates computations by gates and communications by nets. Netlist: a data structure to represent circuits in programs Constructed from components and wires in the EasyVL file Provide necessary information to the logic simulation algorithm

30 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 30/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

31 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 31/64 Class Design for Complex Systems So far we have some experiences in designing a single class. For a complex system, the unit of design is a collection of classes, rather than an individual class. A complex system usually involves many different objects interacting with each other, requiring to compose classes/objects for ownership and beyond. Such a collection is often called a library or a framework, and is also the unit of reuse and maintenance. Though class design depends on the system itself, we can follow object-oriented design (OOD) practices. An effective method in the very beginning of OOD. Describe the system in natural language Nouns are candidates of classes. Verbs give hints to class responsibilities and members. Further improvements can be made based on class invariants and invariants between classes.

32 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 32/64 What is a Netlist? Gates and nets Gates are constructed from components, e.g. xor Nets are constructed from wires we use the word net here since in EasyVL a wire can pass multiple bits of signals. Gate contains pins Nets connect to a gate through its pins. Different pins have different functionality according to their order in the gate. A net connects to multiple gates through multiple pins. We identify the gate driving the net and the gates driven by the net by deciding the direction of the pin, i.e. output and input. The netlist is the collection of the gates/nets/pins. This is usually known as structural representation of a circuit.

33 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 33/64 Entity-Relationship (ER) Diagram We use ER diagram to visualize the structural representation. ER diagrams are widely used for database designs otherwise. Entities (box), relationships (diamond), attributes (oval).

34 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 34/64 Class Design Ideas Each entity corresponds to a class type. Use data members to store attributes. Use additional data members to store directly associated relationships and their attributes. Use getters/setters to access attributes and relationships.

35 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 35/64 Possible Class Design (with Some Issues) Name every pin objects. For relationship gate contains pins, Store in each gate object a vector of the names of the pin objects. Store in each pin object the name of the gate it belongs to and an index of the relationship. For relationship net connects pins, Store in each net object a vector/list of the names of the pin objects. Store in each pin object the name of the net it connects to. The netlist class holds three containers to store the gate/net/pin objects. To access a specific gate/net/pin object, simply search through the containers to find it by name. Any problem?

36 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 36/64 Improvements Search by name is too slow, even if you use std::maps in netlist to hold gate/net/pin objects. Need O(log n) time to access each related object. To avoid repeated search, we use pointers to refer to objects instead of using their names. Search once when creating the object and store the results as pointers. Allow to access a related object in O(1) time.

37 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 37/64 Class Design class net { std::string name_; // e.g. "a", "s[0]" char signal_; // e.g. 0 or 1 std::list<pin *> connections_; // relationship "connect" ; // class net class pin { char dir_; // e.g. I or O gate *gate_; // relationship "contain" size_t index_; // attribute of "contain" net *net_; // relationship "connect" ; // class pin class gate { std::string name_; std::string type_; // e.g. "and", "or" std::vector<pin *> pins_; // relationship "contain" ; // class gate class netlist { std::list<gate *> gates_; std::list<net *> nets_; ; // class netlist

38 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 38/64 What the netlist looks like for this circuit? // a comment module top; wire s0, s1, clk; evl_clock(clk); evl_dff(s0, s1, clk); not(s1, s0); endmodule

39 The Netlist ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 39/64

40 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 40/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

41 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 41/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

42 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 42/64 Cyclic Dependencies among Types class A { B *b_; ; // class A class B { A *a_; ; // class B C++ requires a type to be defined before being used. The above code won t compile since B is not defined when we define A. It won t help if we define B first, since then A is not defined yet when we define B. However, defining A and B in such a way does make sense. Especially when there are many objects interacting with each other.

43 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 43/64 Forward Declarations class B; // forward declaration class A { B *b_; ; // class A class B { A *a_; ; // class B Since A only holds a pointer to B, the compiler doesn t need to know the detailed implementations of B when defining A. In such case, the compiler only need to know that B is a class. Through a forward declaration of B.

44 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 43/64 Forward Declarations class B; // forward declaration class A { B *b_; ; // class A class B { A *a_; ; // class B Since A only holds a pointer to B, the compiler doesn t need to know the detailed implementations of B when defining A. In such case, the compiler only need to know that B is a class. Through a forward declaration of B.

45 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 44/64 Some Cases Cannot Be Resolved by Forward Declarations class B; class A { B b_; ; // class A class B { A a_; ; // class B It doesn t make sense for A to have a member of type B and for B to have a member of type A at the same time.

46 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 45/64 Updated Class Design with Forward Declarations // netlist.h #ifndef GUARD_NETLIST_H #define GUARD_NETLIST_H class netlist; class gate; class net; class pin; class net {... ; // class net class pin {... ; // class pin class gate {... ; // class gate class netlist {... ; // class netlist #endif

47 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 46/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

48 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 47/64 Creating Netlist How to create a netlist depends on the information that is readily available. I.e. the objects we created in Project 1 and 2. A netlist object can be created as follows Create net objects from wires Create gate objects from components Create pin objects within each gate Make connections between pins and nets Validate semantics as necessary.

49 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 48/64 A Possible Interface to Project 1 and 2 struct evl_wire { std::string name; int width; ; // struct evl_wire typedef std::list<evl_wire> evl_wires; struct evl_pin { std::string name; int bus_msb, bus_lsb; ; // struct evl_pin typedef std::list<evl_pin> evl_pins; struct evl_component { std::string type, name; evl_pins pins; ; // evl_component typedef std::list<evl_component> evl_components; bool parse_evl_file(std::string evl_file, std::string &module_name, evl_wires &wires, evl_components &comps); Assume everything we completed in Project 1 and 2 is hidden inside the function parse_evl_file.

50 steps to create the netlist. ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 49/64 Interface Design for netlist int main(int argc, char *argv[]) {... // validate arguments std::string module_name; evl_wires wires; evl_components comps; if (!parse_evl_file(argv[1], module_name, wires, comps)) return -1; evl_wires_table wires_table; if (!make_wires_table(wires, wires_table)) return -1; netlist nl; if (!nl.create(wires, comps, wires_table)) return -1; std::string nl_file = std::string(argv[1])+".netlist"; nl.save(nl_file, module_name); // save the netlist for Project 3 nl.simulate(1000); // simulate 1000 cycles for Project 4 return 0; Let s assume the member function create will take care of all

51 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 50/64 Implement netlist::create bool netlist::create(const evl_wires &wires, const evl_components &comps, const evl_wires_table &wires_table) { return create_nets(wires) && create_gates(comps, wires_table); Let s take a top-down approach. Divide responsibilities into smaller pieces until you know how to implement them. Hand over the creation to the other two member functions. They should be private as they are not supposed to be used outside the class. It makes sense to create nets first since they are needed when connecting pins to nets. Note that short-circuit evaluation ensures if create_nets fails, then create_gates would NOT be called.

52 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 51/64 Implement netlist::create nets bool netlist::create_nets(const evl_wires &wires) { for each wire w in wires { if (width of w == 1) { create_net(w); else { for (int i = 0; i < width of w; ++i) { create_net(w[i]); Let s simplify our slides by using pseudo code. We further delegate the creation of a net object to another member function create_net. When connecting pins to nets, we need to find nets by their names. How to generate the name w[i] of each net as a string? How to search efficiently?

53 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 52/64 Generate Net Names std::string make_net_name(std::string wire_name, int i) { assert(i >= 0); std::ostringstream oss; oss << wire_name << "[" << i << "]"; return oss.str(); We can use std::ostringstream, which allows to save the stream output to a string. From the standard header sstream

54 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 53/64 Implement netlist::create net class netlist {... std::map<std::string, net *> nets_table_;... ; // class netlist void netlist::create_net(std::string net_name) { assert(nets_table_.find(net_name) == nets_table_.end()); net *n = new net(net_name); nets_table_[net_name] = n; nets_.push_back(n); net objects are created on the heap. Use an associative container to facilitate search. You would need to implement ctor of the net class by yourself.

55 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 54/64 Implement netlist::create gates and netlist::create gate bool netlist::create_gates(const evl_components &comps, const evl_wires_table &wires_table) { for each component c in comps { create_gate(c, wires_table); bool netlist::create_gate(const evl_component &c, const evl_wires_table &wires_table) { gate *g = new gate; gates_.push_back(g); return g->create(c, nets_table_, wires_table); Similar to netlist, we choose to create a gate (create pins and make connections) using its member function create instead of using a ctor. It is necessary to provide wires_table for semantics of pins and nets_table_ for making connections between pins and nets.

56 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 55/64 Implement gate::create and gate::create pin bool gate::create(const evl_component &c, const std::map<std::string, net *> &nets_table, const evl_wires_table &wires_table) { set gate type and name; size_t index = 0; for each evl_pin ep in c { create_pin(ep, index, nets_table, wires_table); ++index; return validate_structural_semantics(); bool gate::create_pin(const evl_pin &ep, size_t index, const std::map<std::string, net *> &nets_table, const evl_wires_table &wires_table) { resolve semantics of ep using wires_table pin *p = new pin; pins_.push_back(p); return p->create(this, index, ep, nets_table); Similar to gate, we create a pin (make connections to nets) using its member function create.

57 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 56/64 Implement pin::create and net::append pin bool pin::create(gate *g, size_t index, const evl_pin &p, const std::map<std::string, net *> &nets_table) { store g and index; if (p.msb == -1) { // a 1-bit wire net_name = p.name; net_ = find net_name in nets_table net_->append_pin(pin); else { // a bus... void net::append_pin(pin *p) { connections_.push_back(p); What if the pin connects to a bus? Don t worry about this until you have passed many golden tests. You will need to modify the ER diagram and then the class design to accommodate this feature.

58 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 56/64 Implement pin::create and net::append pin bool pin::create(gate *g, size_t index, const evl_pin &p, const std::map<std::string, net *> &nets_table) { store g and index; if (p.msb == -1) { // a 1-bit wire net_name = p.name; net_ = find net_name in nets_table net_->append_pin(pin); else { // a bus... void net::append_pin(pin *p) { connections_.push_back(p); What if the pin connects to a bus? Don t worry about this until you have passed many golden tests. You will need to modify the ER diagram and then the class design to accommodate this feature.

59 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 57/64 Outline Understand Building The Building Process Header Files and Translation Units Netlist Logic Simulation and Circuit Netlist Object-Oriented Design Netlist Construction Forward Declaration Creating Netlist Destructor and Copy Control

60 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 58/64 No delete? There is nowhere in our code that deletes the nets/gates/pins we created on the heap. Recall it s the responsibility of C++ programmers to destroy the objects on the heap (and return the memory to the heap). When should objects on heap be deleted? It s hard to answer in general and depends on your design. We can always delegate the responsibility to a class. In our design, since the objects are used by many classes, we can assign the responsibility to delete an object to the class that created it. Ownership: netlist owns gates and nets, gate owns pins. This is also the common and recommended practice for C++.

61 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 59/64 Destructors // netlist.cpp gate::~gate() { for (size_t i = 0; i < pins_.size(); ++i) delete pins_[i]; netlist::~netlist() {... // delete pointers stored in gates_ and nets_ The destructor (dtor) is a special member function. Has the name of ~ plus the class name Won t return a result and takes no argument Will be called automatically when the object is destroyed. Unlike ctors, a class has exactly one dtor. Where are the dtors for other classes?

62 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 60/64 Member Destruction and Implicitly-Declared Destructor You don t need to destroy the members explicitly in the dtor. The members are destroyed after the body of the dtor. The compiler will generate code for that purpose the containers in gate and netlist will be destroyed correctly. So you can still use the members in the dtor. Moreover, the compiler will generate an empty public dtor for any non-reference type if the type has no user-defined dtor. For built-in types, it will do nothing. For class types, it will destroy their members the containers in net and pin will be destroyed correctly.

63 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 61/64 Understanding Destruction for Containers What gets destroyed when a container is destroyed? The container and all its elements are destroyed. What if the elements are pointers? The pointers, as objects themselves, are destroyed. The objects pointed by the pointers are not destroyed. Whether those pointers need to be deleted depends on the ownership. So we delete the points from gates_ and nets_ in netlist, and from pins_ in gate, while we don t do anything for those from connections_ in net, and from nets_table_ in netlist.

64 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 62/64 Automatic Object Destruction int main(int argc, char *argv[]) {... netlist nl; if (!nl.create(wires, comps, wires_table)) return -1;... return 0; nl is a local variable and will be destroyed automatically when the function main returns. The dtor of nl is guaranteed to be called automatically at every exit of the function. So all gates and nets will be destroyed. When gates are destroyed, pins will also be destroyed.

65 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 63/64 Copy and Assignment The C++ compiler will implement copy and assignment of objects automatically as we will learn later. However, our current class design does not work well with such automatic implementations. Conceptually, it is a bad idea to make copies of gate, pin, net objects since we have to maintain invariants among them. We will simply disable such implementations like follows. class net { net(const net &) = delete; net &operator=(const net &) = delete;... ; // class net Details will be explained in later lectures.

66 ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 64/64 Summary and Advice Separate compilation supports the separation of interfaces and implementations. In a translation unit, functions should be declared and types should be defined before being used. Use header files to manage function declarations and type definitions Use #include guard in every header file Assign responsibility to each class. Let each class does its own job avoid to expose private data members as much as you can. Plan ahead for allocating objects from heap. Prefer to manage the lifetime of an object on the heap by constructing and destroying it in the same class using ctor/dtor, plus copy control (will be introduced later). Otherwise, use GC (will be introduced later).

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 1/60 ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns Professor Jia Wang Department of Electrical

More information

ECE 449 OOP and Computer Simulation Lecture 08 Resource Management I

ECE 449 OOP and Computer Simulation Lecture 08 Resource Management I ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 1/69 ECE 449 OOP and Computer Simulation Lecture 08 Resource Management I Professor Jia Wang Department of Electrical

More information

ECE 449 OOP and Computer Simulation Lecture 12 Resource Management II

ECE 449 OOP and Computer Simulation Lecture 12 Resource Management II ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 1/62 ECE 449 OOP and Computer Simulation Lecture 12 Resource Management II Professor Jia Wang Department of Electrical

More information

ECE 449 OOP and Computer Simulation Lecture 09 Logic Simulation

ECE 449 OOP and Computer Simulation Lecture 09 Logic Simulation ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 1/31 ECE 449 OOP and Computer Simulation Lecture 09 Logic Simulation Professor Jia Wang Department of Electrical

More information

ECE 449 OOP and Computer Simulation Lecture 14 Final Exam Review

ECE 449 OOP and Computer Simulation Lecture 14 Final Exam Review ECE 449 Object-Oriented Programming and Computer Simulation, Fall 2017, Dept. of ECE, IIT 1/35 ECE 449 OOP and Computer Simulation Lecture 14 Final Exam Review Professor Jia Wang Department of Electrical

More information

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 14 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2016 ENCM 339 Fall 2016 Slide Set 14 slide 2/35

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370

Part VII. Object-Oriented Programming. Philip Blakely (LSC) C++ Introduction 194 / 370 Part VII Object-Oriented Programming Philip Blakely (LSC) C++ Introduction 194 / 370 OOP Outline 24 Object-Oriented Programming 25 Member functions 26 Constructors 27 Destructors 28 More constructors Philip

More information

CS32 Summer Intro to Object-Oriented Programming in C++ Victor Amelkin August 12, 2013

CS32 Summer Intro to Object-Oriented Programming in C++ Victor Amelkin August 12, 2013 CS32 Summer 2013 Intro to Object-Oriented Programming in C++ Victor Amelkin August 12, 2013 History Martin Richards Ken Thompson Dennis Ritchie Bjarne Stroustrup BCPL (1966) B (1970) C (1972-...) C++ (1979-...)

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. What are 3 differences between C and C++ const variables? QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,

More information

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example CS 311 Data Structures and Algorithms Lecture Slides Friday, September 11, 2009 continued Glenn G. Chappell

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

More information

CS61, Fall 2012 Section 2 Notes

CS61, Fall 2012 Section 2 Notes CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors

More information

PIC 10A Objects/Classes

PIC 10A Objects/Classes PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example

More information

Programmazione. Prof. Marco Bertini

Programmazione. Prof. Marco Bertini Programmazione Prof. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Hello world : a review Some differences between C and C++ Let s review some differences between C and C++ looking

More information

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 333 Midterm Exam Sample Solution 7/28/14 Question 1. (20 points) C programming. For this question implement a C function contains that returns 1 (true) if a given C string appears as a substring of another C string starting at a given position.

More information

CS24 Week 3 Lecture 1

CS24 Week 3 Lecture 1 CS24 Week 3 Lecture 1 Kyle Dewey Overview Some minor C++ points ADT Review Object-oriented Programming C++ Classes Constructors Destructors More minor Points (if time) Key Minor Points const Motivation

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

PHY4321 Summary Notes

PHY4321 Summary Notes PHY4321 Summary Notes The next few pages contain some helpful notes that summarize some of the more useful material from the lecture notes. Be aware, though, that this is not a complete set and doesn t

More information

Common Misunderstandings from Exam 1 Material

Common Misunderstandings from Exam 1 Material Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself

More information

Financial computing with C++

Financial computing with C++ Financial Computing with C++, Lecture 6 - p1/24 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 6 - p2/24 Outline Linked lists Linked

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1. COMP6771 Advanced C++ Programming Week 4 Part One: (continued) and 2016 www.cse.unsw.edu.au/ cs6771 2. Inline Constructors, Accessors and Mutators Question (from 2015): In the week 3 examples, constructors

More information

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary GATE- 2016-17 Postal Correspondence 1 C-Programming Computer Science & Information Technology (CS) 20 Rank under AIR 100 Postal Correspondence Examination Oriented Theory, Practice Set Key concepts, Analysis

More information

1 of 8 3/28/2010 8:03 AM C++ Special Topics Home Class Info Links Lectures Newsgroup Assignmen This is a short review of special topics in C++ especially helpful for various assignments. These notes are

More information

CIS 190: C/C++ Programming. Classes in C++

CIS 190: C/C++ Programming. Classes in C++ CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:

More information

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management Session 8 Memory Management The issues Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Programs manipulate data, which must be stored

More information

Objects Managing a Resource

Objects Managing a Resource Objects Managing a Resource 1 What is a Resource Respects Release/Acquire protocol files (open/close) memory allocation (allocate/free) locks (acquire/release). 2 What is a Resource Objects when constructed,

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

SFU CMPT Topic: Classes

SFU CMPT Topic: Classes SFU CMPT-212 2008-1 1 Topic: Classes SFU CMPT-212 2008-1 Topic: Classes Ján Maňuch E-mail: jmanuch@sfu.ca Friday 15 th February, 2008 SFU CMPT-212 2008-1 2 Topic: Classes Encapsulation Using global variables

More information

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s

The pre-processor (cpp for C-Pre-Processor). Treats all # s. 2 The compiler itself (cc1) this one reads text without any #include s Session 2 - Classes in C++ Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) A C++ source file may contain: include directives #include

More information

Today s lecture. CS 314 fall 01 C++ 1, page 1

Today s lecture. CS 314 fall 01 C++ 1, page 1 Today s lecture Midterm Thursday, October 25, 6:10-7:30pm general information, conflicts Object oriented programming Abstract data types (ADT) Object oriented design C++ classes CS 314 fall 01 C++ 1, page

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Lecture 13: more class, C++ memory management

Lecture 13: more class, C++ memory management CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 13:

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.. COMP6771 Advanced C++ Programming Week 5 Part Two: Dynamic Memory Management 2016 www.cse.unsw.edu.au/ cs6771 2.. Revisited 1 #include 2 3 struct X { 4 X() { std::cout

More information

CS11 Intro C++ Spring 2018 Lecture 3

CS11 Intro C++ Spring 2018 Lecture 3 CS11 Intro C++ Spring 2018 Lecture 3 C++ File I/O We have already seen C++ stream I/O #include cout > name; cout

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

CSE 333 Midterm Exam Sample Solution 5/10/13

CSE 333 Midterm Exam Sample Solution 5/10/13 Question 1. (18 points) Consider these two C files: a.c void f(int p); int main() { f(17); return 0; b.c void f(char *p) { *p = 'x'; (a) Why is the program made from a.c and b.c incorrect? What would you

More information

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam July 24, Name UW ID# Name UW ID# There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,

More information

ECE 449, Fall 2017 Computer Simulation for Digital Logic Project Instruction

ECE 449, Fall 2017 Computer Simulation for Digital Logic Project Instruction ECE 449, Fall 2017 Computer Simulation for Digital Logic Project Instruction 1 Summary We present in this document the project instruction for ECE 449 including the grading policy. Our goal is to build

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

C++ Constructor Insanity

C++ Constructor Insanity C++ Constructor Insanity CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon

More information

Ch. 11: References & the Copy-Constructor. - continued -

Ch. 11: References & the Copy-Constructor. - continued - Ch. 11: References & the Copy-Constructor - continued - const references When a reference is made const, it means that the object it refers cannot be changed through that reference - it may be changed

More information

ADTs in C++ In C++, member functions can be defined as part of a struct

ADTs in C++ In C++, member functions can be defined as part of a struct In C++, member functions can be defined as part of a struct ADTs in C++ struct Complex { ; void Complex::init(double r, double i) { im = i; int main () { Complex c1, c2; c1.init(3.0, 2.0); c2.init(4.0,

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 12 October 8, 2018 CPSC 427, Lecture 12, October 8, 2018 1/24 Uses of Pointers Feedback on Programming Style CPSC 427, Lecture 12, October

More information

Class Destructors constant member functions

Class Destructors constant member functions Dynamic Memory Management Class Destructors constant member functions Shahram Rahatlou Corso di Programmazione++ Roma, 6 April 2009 Using Class Constructors #include Using std::vector; Datum average(vector&

More information

Operator overloading

Operator overloading 1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The

More information

QUIZ. Source:

QUIZ. Source: QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Ch. 4: Data Abstraction The only way to get massive increases in productivity is to leverage off other people s code. That

More information

Lecture 14: more class, C++ streams

Lecture 14: more class, C++ streams CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 14:

More information

Homework 4. Any questions?

Homework 4. Any questions? CSE333 SECTION 8 Homework 4 Any questions? STL Standard Template Library Has many pre-build container classes STL containers store by value, not by reference Should try to use this as much as possible

More information

Structuur van Computerprogramma s 2

Structuur van Computerprogramma s 2 Structuur van Computerprogramma s 2 dr. Dirk Deridder Dirk.Deridder@vub.ac.be http://soft.vub.ac.be/ Vrije Universiteit Brussel - Faculty of Science and Bio-Engineering Sciences - Computer Science Department

More information

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I Review from Lecture 2 CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I Vectors are dynamically-sized arrays Vectors, strings and other containers should be: passed by reference when they are to

More information

Allocation & Efficiency Generic Containers Notes on Assignment 5

Allocation & Efficiency Generic Containers Notes on Assignment 5 Allocation & Efficiency Generic Containers Notes on Assignment 5 CS 311 Data Structures and Algorithms Lecture Slides Friday, October 30, 2009 Glenn G. Chappell Department of Computer Science University

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

EL2310 Scientific Programming

EL2310 Scientific Programming (pronobis@kth.se) Overview Overview Wrap Up Introduction to Object Oriented Paradigm More on and Members Operator Overloading Last time Intro to C++ Differences between C and C++ Intro to OOP Today Object

More information

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; } Multiple choice questions, 2 point each: 1. What output is produced by the following program? #include int f (int a, int &b) a = b + 1; b = 2 * b; return a + b; int main( ) int x=1, y=2, z=3;

More information

EL2310 Scientific Programming

EL2310 Scientific Programming Lecture 14: Object Oriented Programming in C++ (fpokorny@kth.se) Overview Overview Lecture 14: Object Oriented Programming in C++ Wrap Up Introduction to Object Oriented Paradigm Classes More on Classes

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference

More information

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

G52CPP C++ Programming Lecture 7. Dr Jason Atkin G52CPP C++ Programming Lecture 7 Dr Jason Atkin 1 This lecture classes (and C++ structs) Member functions inline functions 2 Last lecture: predict the sizes 3 #pragma pack(1) #include struct A

More information

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I Polynomial Class Polynomial(); Polynomial(const string& N, const vector& C); Polynomial operator+(const Polynomial& RHS) const; Polynomial operator-(const Polynomial& RHS) const; Polynomial operator*(const

More information

Lecture 10: building large projects, beginning C++, C++ and structs

Lecture 10: building large projects, beginning C++, C++ and structs CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 10:

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 3 September 7, 2016 CPSC 427, Lecture 3 1/27 Insertion Sort Example Program specification Monolithic solution Modular solution in C Modular

More information

G52CPP C++ Programming Lecture 13

G52CPP C++ Programming Lecture 13 G52CPP C++ Programming Lecture 13 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture Function pointers Arrays of function pointers Virtual and non-virtual functions vtable and

More information

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions CS162: Introduction to Computer Science II A typical way to handle error conditions is through the return value. For example, suppose we create a loadfile() function that returns true if it loaded the

More information

Introduction Of Classes ( OOPS )

Introduction Of Classes ( OOPS ) Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

III. Classes (Chap. 3)

III. Classes (Chap. 3) III. Classes III-1 III. Classes (Chap. 3) As we have seen, C++ data types can be classified as: Fundamental (or simple or scalar): A data object of one of these types is a single object. int, double, char,

More information

CSE 333 Lecture smart pointers

CSE 333 Lecture smart pointers CSE 333 Lecture 14 -- smart pointers Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington Administrivia New exercise out today, due Wednesday morning Exam Friday

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

MODERN AND LUCID C++ ADVANCED

MODERN AND LUCID C++ ADVANCED Informatik MODERN AND LUCID C++ ADVANCED for Professional Programmers Prof. Peter Sommerlad Thomas Corbat Director of IFS Research Assistant Rapperswil, FS 2016 LIBRARY API/ABI DESIGN PIMPL IDIOM HOURGLASS

More information

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too) HW6 NOTE: Do not use the STL map or STL pair for HW6. (It s okay to use them for the contest.)

More information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011 CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture

More information

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2 Steven J. Zeil November 13, 2013 Contents 1 Destructors 2 2 Copy Constructors 11 2.1 Where Do We Use a Copy Constructor? 12 2.2 Compiler-Generated Copy Constructors............................................

More information

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

CSE au Midterm Exam Nov. 2, 2018 Sample Solution Question 1. (16 points) Build tools and make. We re building a C++ software back-end prototype for a new food web site. So far, we ve got the following source files with the code for two main programs

More information

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int, Classes Starting Savitch Chapter 10 l l A class is a data type whose variables are objects Some pre-defined classes in C++ include int, char, ifstream Of course, you can define your own classes too A class

More information

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26 COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47 Lecture 8 Xiaoguang Wang STAT 598W February 13th, 2014 (STAT 598W) Lecture 8 1 / 47 Outline 1 Introduction: C++ 2 Containers 3 Classes (STAT 598W) Lecture 8 2 / 47 Outline 1 Introduction: C++ 2 Containers

More information

Intermediate C++ 1/83

Intermediate C++ 1/83 Intermediate C++ 1/83 Sections I. Memory Management Basics II. The C++ Standard Library III. Casting IV. Resource Management: RAII 2/83 I. Memory Management Basics 1. Checking for memory leaks 2. Pass

More information

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes COMP26120: Algorithms and Imperative Programming Lecture 5: Program structuring, Java vs. C, and common mistakes Lecture outline Program structuring Functions (defining a functions, passing arguments and

More information

C++ Tutorial AM 225. Dan Fortunato

C++ Tutorial AM 225. Dan Fortunato C++ Tutorial AM 225 Dan Fortunato Anatomy of a C++ program A program begins execution in the main() function, which is called automatically when the program is run. Code from external libraries can be

More information

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

G52CPP C++ Programming Lecture 14. Dr Jason Atkin G52CPP C++ Programming Lecture 14 Dr Jason Atkin 1 Last Lecture Automatically created methods: A default constructor so that objects can be created without defining a constructor A copy constructor used

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

class Polynomial { public: Polynomial(const string& N = no name, const vector<int>& C = vector<int>());... }; Default Arguments 1 When declaring a C++ function, you may optionally specify a default value for function parameters by listing initializations for them in the declaration: class Polynomial { public:

More information

Object-Oriented Programming, Iouliia Skliarova

Object-Oriented Programming, Iouliia Skliarova Object-Oriented Programming, Iouliia Skliarova CBook a = CBook("C++", 2014); CBook b = CBook("Physics", 1960); a.display(); b.display(); void CBook::Display() cout

More information

Object-Oriented Programming

Object-Oriented Programming - oriented - iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018 1 / 56 Overview - oriented 1 2 -oriented 3 4 5 6 7 8 Static and friend elements 9 Summary 2 / 56 I - oriented was initially created by Bjarne

More information

COMP6771 Advanced C++ Programming

COMP6771 Advanced C++ Programming 1.... COMP6771 Advanced C++ Programming Week 3 Part One: - Overview, and 2016 www.cse.unsw.edu.au/ cs6771 2.... What is Object-based Programming? A class uses data abstraction and encapsulation to define

More information

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()

More information