Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-22-classes

 ics212-22-classes

Avatar for William Albritton

William Albritton

November 03, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  Overview of C++  Compiling C++ programs

     Input & output in C++  Class definition basics  Member functions and static data members
  2. C++ History  Developed by Bjarne Stroustrup at Bell Labs

    in early 1980’s  From C and Simula-67  Originally called “C with classes”  Later changed to “C++”
  3. What is C++?  C++ is an enhanced version of

    C  Object-oriented-programming capabilities  Other improvements on C features  Is a “superset” of C  Can compile C programs with C++ compiler
  4. Procedural Language  C is a procedural programming language 

    Programming is action-oriented  Function is basic unit of programming
  5. OOP Language  C++ is an object-oriented programming language 

    Classes (a user-defined data type) are the basic unit of programming  Attributes are called “data members” (variables)  Behaviors are called “member functions”  Objects are instantiated (created) from the classes
  6. Compiling C++ Programs  Two big changes: 1. In UNIX,

    use “g++” compiler (not “gcc”) 2. For C++ files, end with “.cpp” (not “.c”)
  7. Makefile in C++  Use the g++ compiler  And

    program.cpp file program: program.o g++ program.o -o program program.o: program.cpp g++ -c program.cpp
  8. Hello World Program  Basic C++ program which outputs: Aloha!

     See example program file in Laulima: aloha.cpp /*Will output “Aloha!”*/ #include <iostream> int main(void){ std::cout<<“Aloha!\n”; return 0; }
  9. Error from Not Using g++ Compiler Undefined first referenced symbol

    in file std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const/var/tmp//ccFFfX7X.o std::cout /var/tmp//ccFFfX7X.o
  10. I/O in C++  See example file at: add.cpp 

    Asks the user to enter two doubles, then adds the numbers and displays the sum  Note changes in library, namespace, input, and output in C++
  11. Header Files  Preprocessor directive (occurs before compiling)  Library

    for the input/output stream functions  See link to C++ Standard Library header files  For programmer-defined header files, use double quotes around the name of the .h file:  #include "myHeaderFile.h" #include <iostream>
  12. Standard C++ library namespace  If we don’t use this

    code at the top of our program, we have to wrote “std::” in front of each standard library reserve word (std::cout instead of cout)  In multiple-file programs, namespace is used to avoid naming conflicts using namespace std;
  13. Output with cout<<  Output values to the screen 

    Standard output stream (cout), or “see-out”  Stream insertion operator (<<), or “put to” cout<<“Enter a double: ”;
  14. Input with cin>>  Obtain a value from the keyboard

     Standard input stream (cin), or “see-in”  Stream extraction operator (>>), or “get from” cin>>number1;
  15. Class Definition  Also called an abstract data type (ADT)

     Defines the data members (variables) and member functions of the class  The class instantiates (creates) objects that store data and that also use these functions  See example code at: fraction.cpp
  16. Class Definition Syntax  class NameOfClass{ public: //constructor & member

    functions private: //data members & utility functions }; //don’t forget semicolon at end!
  17. Member Access Specifiers  Specify where a variable or function

    can be used in a program  If no specifiers are declared, then the default is private  Can list specifiers in any order
  18. Member Access Specifiers 1. public: Data members and member functions

    declared below public are accessible anywhere in the program 2. private: Data members and member functions declared below private are only accessible to member functions of the class 3. protected: Used for inheritance
  19. Principle of Least Privilege  Someone should be given no

    more privilege than necessary to perform a job, such as accessing functions and variables  Basically, if you don’t need access, you don’t get access  This is an important concept in software engineering, computer security, and other fields
  20. Principle of Least Privilege  Helps with writing code that

    has less bugs  For example, access to private data members is restricted to member functions, which use the data members in appropriate ways (information hiding)  Less likely a user can crash a program  For example, for the Fraction class, the user cannot set the denominator to zero (0)
  21. Information Hiding  Information hiding (data hiding) is used in

    object- oriented languages to hide the details of how a class works by restricting access to private data members, which can only be manipulated by member functions  Encapsulation is grouping the data members and member functions together into a single class
  22. Steps to Write a Class Definition 1. Class, class name,

    curly brackets, and semicolon 2. Private data members at bottom by convention 3. Public constructor to initialize the data members 4. Public display function to output the data members 5. Driver (main) function with objects and function calls to test the constructor and display function
  23. Example Class Definition & Driver  Class Fraction has private

    data members numerator and denominator, a public constructor and print() function, and a main function as the driver program  Two restrictions:  The denominator cannot be zero: illegal fraction with undefined value  The denominator is always positive by convention
  24. Data Members  Placed in the private section of the

    class definition (information hiding)  Cannot be initialized (only in the constructor)  Start with a lowercase letter  Data member syntax: dataType variableName;
  25. Constructor  Initializes the data members  Placed in the

    public section of the class definition  Starts with a capital letter  Constructor syntax: ClassName(dataType parameters){ //code to initialize data members }
  26. Function to Display Data Members  Placed in the public

    section of the class definition  All functions start with a lowercase letter  Exception is constructors  We are only displaying the data members, so we don’t want to change them (Principle of Least Privilege)
  27. Function to Display Data Members  We use const in

    the function header, so we cannot change the data members by mistake (Principle of Least Privilege)  Output function syntax: returnType function (dataType parameters) const{ //code to display data members that cannot be changed }
  28. Driver to Test Constructor  To test the constructor, we

    need to instantiate (create) objects and call the display function  This is done in the main() function  Syntax for instantiating objects: ClassName objectName;  Syntax for calling a function: objectName.functionName(parameters);
  29. Mutator Functions  A mutator function is a member function

    that changes the values stored in the data members  Usually starts with “set”  Mutator function syntax: void setDataMember(dataType parameters){ dataMember = parameter; }
  30. Accessor Functions  A accessor function is a member function

    that returns the value of a data member  Usually starts with “get”  Accessor function syntax: returnType getDataMember () const{ return dataMember; }
  31. Utility Functions  While most member functions are public, utility

    functions are private  Also called a “helper function”  Supports the operation of member functions  Not intended to be used by the clients of a class  For example, cannot use in the main() function
  32. Static Data Members  Static data members are variables of

    the class that are usually public, constant, and in all capital letters  Public, so can be accessed outside the class in the driver program  Constant, so uses the Principle of Least Privilege  Have to be initialized when declared
  33. Static Data Members  Static data member declaration syntax: static

    const dataType VARIABLE_NAME = value;  Static data member access syntax: variable = ClassName::VARIABLE_NAME; variable = object.VARIABLE_NAME;  See example program at: functions.cpp
  34. Memory Management  Overview of C++  Compiling C++ programs

     Input & output in C++  Class definition basics  Member functions and static data members