1 / 17

Basic C++

Basic C++. State the difference between a function/class declaration and a function/class definition. Explain the purpose of a C++ declaration. Provide an example of a a class or function declaration.

mackr
Télécharger la présentation

Basic C++

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. Basic C++ • State the difference between a function/class declaration and a function/class definition. • Explain the purpose of a C++ declaration. • Provide an example of a a class or function declaration. • Explain the function of the various preprocessor directives, include #include, #define, #ifdef, #if, and #undef CS-1030 Dr. Mark L. Hornick

  2. Basic C++ • Given source files containing preprocessor directives like #ifdef and #define, describe what output the running program will produce. • Be able to write a simple C++ program that produces formatted output. • Describe what values C++ assigns to unitialized variables • Explain the concept of method or function overloading (same name, different number or datatypes of arguments) • Give an example of an overloaded method or function CS-1030 Dr. Mark L. Hornick

  3. Basic C++ • Explain when a constructor is called • Explain when a destructor is called • Discuss how copy constructors are invoked when passing objects as parameters or return values in methods. • Explain which class methods (default and copy constructor, destructor, and operator=) the C++ compiler supplies for you if you don’t explicitly implement those. • Explain which constructor is called when assigning the value of one object to another (copy constructor) • Explain why you would use a destructor CS-1030 Dr. Mark L. Hornick

  4. Using C++ • Given a UML class diagram, write a header (.h) file • Given a header file, draw a UML Class diagram • Given source (.cpp) files, draw a UML Sequence diagram • Given a UML Sequence diagram, write the corresponding C++ statements CS-1030 Dr. Mark L. Hornick

  5. Pointers • Explain what a pointer is • Explain what a pointer contains • Give an example of how to declare a pointer to an item – i.e. primitive or object • Give an example of how to set a pointer to point to an item • Give an example of how to use a pointer to an item to get the value of that item • Given a pointer to an object, give an example of how to access a member function f() of that object • Give an example of how to use the & operator to get the address of an object and assign that address to a pointer • Give an example of how to use the * operator to dereference a pointer; that is, how to get to the object pointed to CS-1030 Dr. Mark L. Hornick

  6. References • Describe the differences and similarities between references and pointers • Explain what a reference contains • Give an example of how to declare a reference to an item - primitive or object • Give an example of how to set a reference to refer to an item • Give an example of how to use a reference to an item to get the value of that item • Given a reference to an object, give an example of how to access a member function f() of that object CS-1030 Dr. Mark L. Hornick

  7. Parameter PassingReturn values • Give examples of passing an object - in a method or function call - by value, by pointer, and by reference • Explain what happens when an object is passed by value, and what is actually passed to the function or method (the object’s copy constructor is automatically invoked, and a copy of the object is passed) • Give examples of returning an object - from a method or function call - by value, by pointer, and by reference • Explain what happens when an object is returned by value from a method (the object’s copy constructor is automatically invoked, and a copy of the object is returned; the object within the method goes out of scope and its destructor is called) • Explain what happens when an object is passed by address or by reference, and what is actually passed to the function or method (a copy of the pointer or reference is made and passed; these copies contain the same address as the original pointer or reference and so point to the original object – no copy of the object is made) • Explain what happens when an object is returned by address or by reference from a method or function (a copy of the pointer or reference is made and returned; these copies contain the same address as the original pointer or reference and so point to the original object – no copy of the object is made. If the object was created as a local variable within the method, the compiler will issue an error because the object gets destroyed when the method returns and the object goes out of scope.) CS-1030 Dr. Mark L. Hornick

  8. Use of const keyword • Describe the effect of const, when used in a variable declaration (the variable cannot change value) • Demonstrate how an initializer must be used to set the initial value of a class’s const data member • Describe the effect of const, when used in a method declaration (the method cannot change the value of any data members of the class to which the method belongs) • Describe the effect of const, when used on a method parameter declaration (the parameter’s value cannot be changed within the method) • Explain the difference between a const pointer to an object and pointer to a const object (a const pointer cannot be changed to point to another object, while a pointer to a const object implies that the object cannot be changed; i.e. methods that change the object’s state cannot be called via the pointer) • Explain the effect of declaring a const pointer to a const object (neither the pointer nor the object can be changed) CS-1030 Dr. Mark L. Hornick

  9. Static data and static methods • Explain the concept of a static class data member (a static data member is shared by all instances of the class) • Explain the concept of a static class method (the method can only access static data members) • Give examples of declaring both static data members and static methods • Give examples of invoking static class methods CS-1030 Dr. Mark L. Hornick

  10. Dynamic Memory • Explain the purpose of the new() operator (allocates heap memory for an object and creates an instance of that object at that memory location) • Explain what the new() operator returns (address of the object it created) • Give an example of using the new() operator and assigning its return value (a memory address) to a pointer of the appropriate type • Give an example of how you would later use the delete() operator to destroy the object and deallocate the memory • Explain what method (the destructor) is invoked as a consequence of calling delete() • Explain what happens if you forget to delete() an object created by new(), or accidentally reassign or otherwise “lose” the pointer pointing to the object (memory is “orphaned” or “leaked” ) CS-1030 Dr. Mark L. Hornick

  11. Inheritance • Demonstrate how to declare that a class inherits, or derives, behavior from another • Explain the order of construction (base class data member ctors, base class ctor, derived class data member ctors, derived class ctor) and destruction (just the opposite) in a derived class • Compare the effects of inheriting using public, protected, and private • Explain which base class constructor is invoked regardless of which constructor is used to construct a derived class (the base class default constructor is invoked unless a base class initializer is explicitly invoked) • Explain what it means to override a method of a base class in a derived class CS-1030 Dr. Mark L. Hornick

  12. Polymorphism • Explain what the virtual keyword means when applied to a method of a (parent) class • Explain polymorphism • Identify polymorphic behavior in sample code • Explain the concept of a pure virtual method in a base class (that method must be given implementation in a derived class in order to make the derived class instantiable) • Demonstrate how to modify a class declaration to make the class abstract (by declaring at least one method to be pure virtual) CS-1030 Dr. Mark L. Hornick

  13. Friends • Explain what it means if a global function is declared to be a friend of a class • Demonstrate how to declare a global function that is a friend to a class • Explain the meaning of declaring an entire class to be a friend of another class • Demonstrate how to declare a class that is a friend to another class CS-1030 Dr. Mark L. Hornick

  14. Operator Overloading • Explain the concept of operator overloading • Demonstrate how to declare an operator method • Demonstrate how to define an operator method • Explain the difference between a binary and unary operator • Be able to identify automatic type conversion • Explain the concept of automatic type conversion CS-1030 Dr. Mark L. Hornick

  15. Templates • Explain the concept of a class template • Explain how templated class method definitions must be implemented (inline within the templated class declaration) • Give an example of how to write a simple templated function such as template <class T> T Max (const T& a, const T& b) { return (a < b) ? b : a; } • Given a templated class, demonstrate how to define an instance of that class for a specific type CS-1030 Dr. Mark L. Hornick

  16. C++ Exceptions • Explain the concept of C++ exception handling • Give an example of using exception handling to trap an error CS-1030 Dr. Mark L. Hornick

  17. Namespaces • Explain the concept of a namespace • Given an example of using a namespaced class CS-1030 Dr. Mark L. Hornick

More Related