1 / 27

Object-Oriented Programming Using C++

Object-Oriented Programming Using C++. CLASS 1. Review of Syllabus Catalog Description

miracle
Download Presentation

Object-Oriented Programming Using 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. Object-Oriented Programming Using C++ CLASS 1

  2. Review of Syllabus • Catalog Description • An introduction to object oriented programming techniques using C++ programming language. Topics covered will include classes, both single and multiple inheritance, templates, polymorphism, exception handling and file processing.

  3. Review of Syllabus • Textbook Data Abstraction & Problem Solving with C++ by Frank Carrano, 5th edition • InstructorKamran KhanOffice Hours kkhan@engr.smu.edu

  4. Review of Syllabus • Catalog Description • Course Requirements and Method of Evaluation • Test #1 125 • Test #2 125 • Test #3 125 • Homework 10 each • Programs 30 each • Pop Quizzes 15 each • Project 100

  5. Grading Example • 1200 possible points • Highest score of 1150 • You collected 920

  6. x 100 920 1150 = 80 B 92000 9200 1150 • Grading Example • 1200 possible points • Highest score of 1150 • You collected 920

  7. Course Competencies • Use the C++ preprocessor • Explain the use of overloading operators • Design and code an inheritance hierarchy

  8. Course Competencies • Explain the use of overloading of function names • Create a library of functions which can process data as objects • Understand memory management and dynamic allocation • Use a virtual function to demonstrate polymorphism

  9. Course Competencies • Demonstrate a knowledge of an “is a” and “has a” relationship between classes • Write a C++ program using sequential and random file processing • Use exception handling techniques • Write a class template and a driver to use it

  10. Assignments L: Programs due Monday 10pm of the week assigned, uploaded into your Blackboard folder the following files: All source (.cpp) files UML files All header (.h) filesExecutable (.out) file H: Homework due at the beginning of class lecture, Thursday, can be hand-written

  11. Late Assignments Homework assignments will not be accepted late Labs (programs) can be turned in the 48 hours late, by special permission/request for a late folder approved by instructor; 10 late points will be deducted.

  12. Explanation of Transmission of Assignments • Programming assignments will be submitted via Blackboard • Next week’s lab will take you through the requirements for Blackboard, Unix logins, Unix executable files, and other information related to your programming assignmentsATTENDANCE IS MANDATORY

  13. Scholastic Dishonesty Policy • Outside assistance other than from your ta, instructor, or university tutoring service is considered scholastic dishonesty • Looking at/copying from another student’s work is considered scholastic dishonesty • A single incident results in a zero for that work for yourself and the person you copied from; multiple incidents results in an F in the class and a report to the Honors Council

  14. Introduction to C++ Classes • Encapsulation combines an ADT’s data with its operations to form an object • An object is an instance of a class • A class defines a new data type • A class contains data members and methods (member functions) • By default, all members in a class are private • But you can specify them as public • Encapsulation hides implementation details

  15. C++ Classes Figure 3-10 An object’s data and methods are encapsulated

  16. C++ Classes vs Java classes • Each class definition is placed in a header file • Classname.h • The implementation of a class’s methods are placed in an implementation file • Classname.cpp

  17. C++ Classes: The header file /** @file Sphere.h */ const double PI = 3.14159; class Sphere {public: Sphere(); // Default constructor Sphere(double initialRadius); // Constructor void setRadius(double newRadius); double getRadius() const; // can’t change data members double getDiameter() const; double getCircumference() const; double getArea() const;

  18. C++ Classes: The header file double getVolume() const; void displayStatistics() const; private: double theRadius; // data members should be private }; // end Sphere interface file

  19. C++ Classes: Constructors • Constructors • Create and initialize new instances of a class • Invoked when you declare an instance of the class • Have the same name as the class • Have no return type, not even void • A class can have several constructors • A default constructor has no arguments • The compiler will generate a default constructor if you do not define any constructors

  20. C++ Classes: Constructors • The implementation of a method qualifies its name with the scope resolution operator :: • The implementation of a constructor • Sets data members to initial values • Can use an initializer Sphere::Sphere() : theRadius(1.0) { } // end default constructor • Cannot use return to return a value

  21. C++ Classes: Destructors • Destructor • Destroys an instance of an object when the object’s lifetime ends • Each class has one destructor • For many classes, you can omit the destructor • The compiler will generate a destructor if you do not define one • For now, we will use the compiler’s destructor

  22. C++ Classes: The implementation file /** @file Sphere.cpp */ #include <iostream> #include "Sphere.h" // header file using namespace std; Sphere::Sphere() : theRadius(1.0) { } // end default constructor Sphere::Sphere(double initialRadius) { if (initialRadius > 0) theRadius = initialRadius; else theRadius = 1.0; } // end constructor

  23. C++ Classes: The implementation file void Sphere::setRadius(double newwRadius) { if (newRadius > 0) theRadius = newRadius; else theRadius = 1.0; } // end setRadius • The constructor could call setRadius

  24. C++ Classes: The implementation file double Sphere::getRadius() const { return theRadius; } // end getRadius . . . double Sphere::getArea() const { return 4.0 * PI * theRadius * theRadius; } // end getArea . . .

  25. C++ Classes: Using the class Sphere #include <iostream> #include "Sphere.h" // header file using namespace std; A mechanism for logically grouping declarations and definitions into a common declarative region • Items declared in the C++ Standard Library are declared in the std namespace • You include files for several functions declared in the std namespace • To include input and output functions from the C++ library, write #include <iostream> usingnamespace std;

  26. C++ Classes: Using the class Sphere int main() // the client { Sphere unitSphere; Sphere mySphere(5.1); cout << mySphere.getDiameter() << endl; // C++’s version of screen output // more on cout later } // end main

  27. Q & A

More Related