1 object-oriented programming using c++ class 1. 2 review of syllabus catalog description –an...
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 [email protected]
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
• Grading Example• 1200 possible points• Highest score of 1150• You collected 920
9201150
x100=
1150 920009200
80 B
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) filesUML filesAll 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
1414
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
1515
C++ Classes
Figure 3-10An object’s data and methods are encapsulated
1616
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
1717
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;
1818
C++ Classes: The header file
double getVolume() const; void displayStatistics() const;private: double theRadius; // data members should be private}; // end Sphere interface file
1919
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
2020
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
2121
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
2222
C++ Classes: The implementation file/** @file Sphere.cpp */#include <iostream>#include "Sphere.h" // header fileusing namespace std;Sphere::Sphere() : theRadius(1.0){} // end default constructorSphere::Sphere(double initialRadius){ if (initialRadius > 0) theRadius = initialRadius; else theRadius = 1.0;} // end constructor
2323
C++ Classes: The implementation filevoid Sphere::setRadius(double newwRadius){ if (newRadius > 0) theRadius = newRadius; else theRadius = 1.0;} // end setRadius
• The constructor could call setRadius
2424
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. . .
2525
C++ Classes: Using the class Sphere
#include <iostream>#include "Sphere.h" // header fileusing 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>using namespace std;
2626
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