333Test1

  1. how do you create a struct?
    • struct EmpRec
    • {
    •     int empID;
    •     string name;
    •     double salary;
    • };
  2. How do you create a class?
    • in headingFile.h
    • class Employee
    • {public:
    •     Employee(string /*input file*/, int = SIZE/*max number of employees*/);
    •     void showEmpRec(int /*employee ID*/) const;
    •     void writeEmployees(ostream & /*output*/) const; 
    •    int getNoEmps() const { return noEmps; }
    •     void addEmp(int /*employee ID*/, string /*name*/, double/*salary*/);
    • private:
    •     int maxEmps;
    •     int noEmps;
    •     EmpRec employee[SIZE];
    •     int findEmp(int /*employee ID*/) const;
    • };
  3. create a class for time
    • class Time
    • {public:
    •     Time(int = 12/*hours*/, int = 0 /*minutes*/, int = 0 /*seconds*/);
    •     void setTime(int,/*hours*/ int,/*minutes*/ int/*secs*/);
    •     void showTime(ostream &) const;
    •     int getHour() const{ return hour; }
    •     int getMinute() const { return minute; }
    •     int getSecond() const { return second; }
    • private:
    •     int hour, minute, second;
    • };
  4. The time constructor prototype is
    Time(int = 12/*hours*/, int = 0 /*minutes*/, int = 0 /*seconds*/);
    • #include "time.h"
    • void Time::setTime(int newHours, int newMinutes, int newSecs)
    • {
    •     hour = newHours > 0 && newHours <= 12 ? newHours : 12;
    •     minute = newMinutes > 0 && newMinutes <60 ? newMinutes : 0;
    •     second = newSecs < 0 || newSecs >= 60 ? 0 : newSecs;
    • }
    • Time::Time(int newHours, int newMinutes, int newSecs)
    • {
    •     setTime(newHours, newMinutes, newSecs);
    • }
  5. Create an array that store empId, name and salary.
    • struct empRec
    • {
    •     int empID,
    •    string name,
    •    double salary,
    • };
    • empRec staff[10];
  6. structure.member
    • member access operator
    • dot operator
  7. What is a class
    Class: programmer-defined data typeAlso called an abstract data typeCombination of data and their associated operations (capabilities)
  8. Class components
    • Two components:
    • Declaration section: declares data types and function for class
    • Implementation section: defines functions whose prototypes were declared in declaration section
  9. Class members:
    • Class members:
    • Data members (instance variables)
    • Member functions
  10. class construction
    • // declaration section
    • class className
    • {
    •    data declarations
    •    function prototypes
    • };
    • // implementation section
    • function definitions
  11. why private
    • private:
    • class members  can only be accessed using class functions
    • Enforces data security (data hiding)
  12. Constructor method:
    • initializes class data members with values
    • Constructor function has same name as the class
    • Constructor function has no return type (a requirement for this special function)
  13. class Date
    {
      private:
        int month, day, year;
      public:
        Date(int =7, int=4, int=2012);
        void setDate(int, int, int);
        void showDate(void);
    };
    Write the constructor.
    • Date::Date(int mm, int dd, int yyyy)
    • {
    •   month=mm;
    •   day=dd;
    •   year=yyyy;
    • }
  14. member function format
    • returnType className::methdoName(parameter list)
    • {
    •    method body
    • }
  15. class Date
    {
      private:
        int month, day, year;
      public:
        Date(int =7, int=4, int=2012);
        void setDate(int, int, int);
        void showDate(void);};
    Write setDate.
    • void Date::setDate(int mm, int dd, int yyyy)
    • {
    •     month=mm;
    •     day=dd;
    •     year=yyyy;
    • }
  16. class Date
    {
      private:
        int month, day, year;
      public:
        Date(int =7, int=4, int=2012);
        void setDate(int, int, int);
        void showDate(void);};
    Write showDate.
    • void Date::showDate()
    • {
    •     cout <<"The date is " << month << "/" << day <<"/" << year;
    • }
  17. ::
    • scope resolution operator
    • to identify function as a member of the class
  18. class Date
    {
      private:
        int month, day, year;
      public:
        Date(int =7, int=4, int=2012);
        void setDate(int, int, int);
        void showDate(void);
    };
    Date::Date(int mm, int dd, int yyyy)
    {  month=mm;  day=dd;  year=yyyy;}
    1. Declare a and b with default values.
    2. Declare c with 4,1,2000
    3.show all 3 dates
    • int main()
    • {
    •   Date a, b, c(4,1,2000);
    •   b.setDate(12,25,2009);
    •   a.showDate();
    •   b.showDate();
    •   c.showDate();
    • }
  19. class Date
    {
    private:
    int month, day, year;
    public:
    Date(int =7, int=4, int=2012);
    void setDate(int, int, int);
    void showDate(void);
    };
    int main()
    {
    Date a, b, c(4,1,2000);
    b.month;
    }
    Is the last line of main valid?
    • No
    • The data member of Date are private
    • month is provide
    • need to use b.showDate();
  20. Instantiation
    • Instantiation: process of creating a new object
    • Creates new set of data members belonging to new object: determines the object’s state
  21. What is the purpose of a constructor?
    When is it used?
    • initialize a new object’s data members
    • Constructors are called whenever an object is created
    • Date c(4,1,2009);
    • (Other methods must be called explicitly by name)
  22. A class declaration must be terminated with a semicolon.
    T or F
    T
  23. A constructor does not need a return type.
    T/F
    T
  24. In the implementation section, the member method needs the class name and scope resolution operator. T/F
    T
  25. What is a constructor?
    1. A constructor is a special function that initializes the data members of a class. It has the same name as the class.
  26. What is an ADT? Why do we use it for our classes?
    • 2. An ADT=Abstract Data Type.
    • We use it to separate the “what” from the “how”. The specification file (“what”) lists the data members and function prototypes and the implementation file (“how”) is the code for the member functions. Only the specification file need be viewed, the implementation file is not viewable, ensuring information hiding,
  27. When is a constructor called?
    3. Constructors are called automatically when an instance of the class is created
  28. A variable of class types is called
    object
  29. In C++, a class constructor without a parameter list is called a(n)
    default constructor.
  30. 7.Distinguish between an object and a class.
    • 7. A class defines what an object can do, an object is an instance of the class.
    • An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.

    A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types.
  31. What is the default access for a class? For a struct?
    8. Default access for a class is private, default access for a struct is public.

    • class uses access operator (.)
    • By default, all members of a class declared with the class keyword have private access for all its members.
  32. 9. What is meant by the term encapsulation?
    9. All of the members of a class are defined in a class and can only be accessed via an instance of the class. Private members can only be accessed by other members of the class.
  33. 10.What is class scope?
    10. All function members of a class can access all the data members. These data members do not have to passed or returned from a member function.
  34. What does the reserved word private indicate within a class?
    11. Private members can only be access by other members in the class. The client code cannot access any private members.
  35. When should you code const?
    12. Code const on member functions when you do not want data members to be changed.
Author
slc53
ID
324609
Card Set
333Test1
Description
333Test1
Updated