14-Objects

# 14-Objects - Very Advanced Advanced Programming defining a...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Very Advanced Advanced Programming defining a “class” of behaviors what's our job? what's iincrementally decompose a problem into a ncrementally code-able solution code-able create a model of that in code the coded model is called a class an instance of the model (a working, real thing) an is called an object is Gas Readout Start Switch N ozzle H andle Dollar Readout Gas Type Flow Meter Gas Pump Motor A 6-jointed Robot Shoulder Elbow Wrist Turn Wrist Bend W aist Grasp http://www.youtube.com/watch?v=ZQG2cRtBCAg encapsulation encapsulation a class / object has: a definition – called a constructor properties – ints, doubles, booleans, strings, properties chars chars behaviors – functions, of the familiar form behaviors return = function( parameters ); a class is like a "mold" class write a class to represent a CIRCLE CIRCLE a definition – requires a radius to begin, definition so a new radius defines a new circle so properties – a radius, which might properties change change behaviors – area and circumference writing a class - Circle.h writing class Circle { public: // 1. definition: Circle( double newRadius ); // 2. variables are properties double radius; // 3. functions are behaviors double getArea( ); double getCircumference( ); }; note semicolon writing a class - Circle.h basic framework for a class class Circle { public: note semicolon }; defining behavior (constructor) (constructor) Circle( double newRadius ); property - variables and constants constants double radius; // property behaviors - functions within the class the what does it do? // functions are behaviors double getArea( ); double getCircumference( ); }; completes the definition completes Here’s the complete class definition (at the top of ) Circle.h class Circle { public: // constructor Circle( double newRadius ); // property double radius; // functions are behaviors double getArea( ); double getCircumference( ); }; finish implementing the class – at the bottom of Circle.h finish Circle::Circle( double newRadius ) Circle( { radius = newRadius; } double Circle::getArea( ) { double return (radius * radius * 3.14159); } double Circle::getCircumference( ) { double return (2 * radius * 3.14159); } ; // semicolon after the last one using it in a main( ) using #include <iostream> #include "Circle.h" using namespace std; int main( ) { Circle circle1(2.0); cout << “circle1 radius: ” << circle1.radius << endl; Circle circle2(5.0); Circle circle3(10.0); this is a Circle circle4(50.0); big deal ! cout << circle1.getArea() << " " << circle1.getCircumference() << endl ; cout << circle2.getArea() << " " << circle2.getCircumference() << endl; cout << circle3.getArea() << " " << circle3.getCircumference() << endl; cout << circle4.getArea() << " " << circle4.getCircumference() << endl; } Circle circle1(2.0); Circle creates a Circle called circle1 with a creates radius of 2.0 radius circle1. circle1. gives access to all of circle1’s things gives circle1.radius circle1.radius circle1.getArea( ) circle1.getCircumference( ) An object is a working example of An the class the Circle circle1(2.0); Circle circle2(5.0); Circle circle3(10.0); Circle circle4(50.0); circle1, circle2, circle3, circle4 are circle1, separate objects of class Circle (so they have dependable behaviors and properties). properties). But they are separate. general form general Class object( initialization ); e.g. Circle circle1( 2.0 ); object.variable; e.g. circle1.radius; object.function( ); e.g. circle1.getArea( ); ...
View Full Document

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern