CS32 Midterm 1 Review Session - CS 32 Midterm 1 Review...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Hosted by Upsilon Pi Epsilon April 20, 2015 CS 32 Midterm 1 Review Session
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Abstract Data Types (ADTs) “A coordinated group of data structures , algorithms and interface functions that is used to solve a particular problem.” – Carey Nachenberg Data structures and algorithms o Kept hidden from the user Interface Functions o Allow the user to interact with the data structures, use the algorithms
Image of page 2
C++ classes In C++, the tool used to create an ADT is called a class class Car { public: // Interface functions go here void getMake(…); double getMPG(…); private: // Algorithms and data structures go here double m_numGallons; void innerWorkingsOfCar(…); }; Since they are in the public section, they can be accessed by anyone Since they are in the private section, only functions in the Car class can access them
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Constructors Special function used to initialize the variables in a class Has the same name as the class Has no return type Default constructor provided iff no user defined constructor exits
Image of page 4
Constructors Special function used to initialize the variables in a class class Car { public: Car ( double gallons, string make, string model) { m_numGallons = gallons; m_make = make; m_model = model; } private: double m_numGallons; string m_make; string m_model; };
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Constructors Initializer Lists o Another way to initialize member variables in the constructor o Useful in class composition class Car { public: Car ( double gallons, string make, string model) { } private: double m_numGallons; string m_make; string m_model; }; Initializer List : m_numGallons(gallons), m_make(make), m_model(model)
Image of page 6
Destructors Special function called whenever the object is destroyed Has the name ~className Has no return type and takes no parameters If not defined by the user, a default one will be used that simply calls the destructors all ADT data members Often used to free dynamically allocated variables
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Destructors Special function called whenever the object is destroyed If not defined by the user, a default one will be used that class Car { public: Car ( double gallons, string make, string model) : m_numGallons(gallons), m_make(make), m_model(model) { } ~Car() { cout << “The car has been destroyed” << endl; } private: double m_numGallons; string m_make; string m_model; };
Image of page 8
Run-through class Engine { public: Engine ( int numCylinders) : m_numCylinders(numCylinders) { cout << “I am an engine with ” ; cout << m_numCylinders + “ cylinders” << endl; } ~Engine() { cout << “Kaboom!” << endl; } private: int m_numCylinders; };
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Run-through class Car { public: Car ( double gallons, string make, string model, int cylinders) : m_engine(cylinders), m_numGallons(gallons), m_make(make), m_model(model) { cout << “A car has been created” << endl; } ~Car() { cout << “The car has been destroyed” << endl; } private: Engine m_engine; double m_numGallons; string m_make; string m_model; };
Image of page 10
Error or no error?
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern