290 likes | 734 Views
Object-Oriented Programming Using C++. CLASS 1. Review of Syllabus Catalog Description
E N D
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.
Review of Syllabus • Textbook Data Abstraction & Problem Solving with C++ by Frank Carrano, 5th edition • InstructorKamran KhanOffice Hours kkhan@engr.smu.edu
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
Grading Example • 1200 possible points • Highest score of 1150 • You collected 920
x 100 920 1150 = 80 B 92000 9200 1150 • Grading Example • 1200 possible points • Highest score of 1150 • You collected 920
Course Competencies • Use the C++ preprocessor • Explain the use of overloading operators • Design and code an inheritance hierarchy
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
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
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
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.
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
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
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
C++ Classes Figure 3-10 An object’s data and methods are encapsulated
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
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;
C++ Classes: The header file double getVolume() const; void displayStatistics() const; private: double theRadius; // data members should be private }; // end Sphere interface file
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
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
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
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
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
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 . . .
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;
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