Pointers - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Pointers

Description:

The easiest is to just compile all the cc's at the same time. CC simpleExample.cc myProgram.cc ... se1.holdNumber=5; //COMPILE TIME ERROR ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 24
Provided by: chrisf2
Category:
Tags: compile | pointers

less

Transcript and Presenter's Notes

Title: Pointers


1
Pointers
  • Class 12 Classes and Object Oriented
    Programming

2
Structs
  • Weve already seen we can create User-Defined
    aggregates of Data Types in structs.
  • We can use this feature to model real world
    objects.
  • Struct Date
  • int month
  • int year
  • int day

3
Classes
  • So what is a class?
  • First, think again about structs they can be
    thought of as Objects that have different
    attributes to them.
  • A Class is an Object that has BOTH attributes
    (data members like ints, floats, even other
    structs and classes) AND behaviors, or
    operations, (member functions functions that
    operate on the data inside the class)

4
Object Oriented Thinking
  • A builder can build a home out of a blueprint.
  • He can also reuse this blueprint many times to
    make many homes.
  • Classes work in kind of the same way.
  • Think of classes as cookie cutters with one
    class, you can stamp out, or create, many
    objects of that class.

5
Using Classes
  • Youve actually (maybe) used classes before, and
    you just didnt know it.
  • From the C style strings from several lectures
    ago, string itself is just a class.
  • You can use classes other people created by
    including them, and you can also write your own
    classes, which we will do shortly.
  • You can use a class very much like a struct..
  • Consider a class with the following class
    declaration

6
Using classes
  • class simpleExample
  • public
  • void setNumber(int)
  • void print()
  • int holdNumber
  • Now lets look at for a minute. It looks exactly
    like a struct, except
  • It says class instead of struct (this is obvious)
  • It has this public thing. This is called an
    access modifier. More on these soon. public
    means it will allow anyone to do anything.
  • Instead of just having data types, it also has
    functions.
  • So lets see how we would use this thing..

7
Using our simple, useless class..
  • We declare it like we do structs
  • simpleExample se1 //instantiate object se1 of
    class simpleExample
  • simpleExample se2 //instantiate object se2 of
    class simpleExample
  • se1.holdNumber 5 //set holdNumber to 5
  • se1.setNumber(84) //calls simpleExamples
    setNumber function
  • se1.print() //calls
    simpleExamples print function
  • So what does setNumber(int) and print() actually
    do? Where are they defined? Inside the Class
    Implementation

8
Class Definitions
  • class simpleExample
  • public
  • void setNumber(int)
  • void print()
  • int holdNumber
  • simpleExamplesetNumber(int anumber)
  • holdNumber anumber
  • simpleExampleprint()
  • cout ltltholdNumber

9
Splitting up Files
  • Having both the Declaration and Implementation in
    one file is not correct.
  • With C, we want to break these up, with the
    Declaration into the header (.h) file and the
    Implementation in the source (.cc) file.

10
Splitting Up Files
  • Inside the .cc File called
  • simpleExample.cc
  • include simpleExample.h
  • simpleExamplesetNumber(int anumber)
  • holdNumber anumber
  • simpleExampleprint()
  • cout ltltholdNumber
  • Inside the .h Filecalled simpleExample.h
  • class simpleExample
  • public
  • void setNumber(int)
  • void print()
  • int holdNumber

11
Compiling
  • Now, if we compile simpleExample.cc, what will
    happen? why?
  • Yes, it will fail. Because what does each
    program need?
  • A starting point a main()
  • So, when we want to use our custom class inside
    one of our programs, we have to do two things.

12
MyProgram
  • In a program, called myProgram.cc, we want to use
    our simpleExample class.
  • We need to include the header file
  • include ltiostreamgt
  • using namespace std
  • include simpleExample.h
  • int main()
  • simpleExample se1
  • se1.setNumber(4)

13
MyProgram
  • Were still not there yet.. we have our program,
    and the header included, but not the
    implementation.
  • We need to link it at runtime. There are a few
    ways to do this. The easiest is to just compile
    all the ccs at the same time.
  • CC simpleExample.cc myProgram.cc

14
MyProgram
  • This isnt necessary always (and can get
    annoying). What the compiler is actually looking
    for is an object file for (.o) for your class.
  • You can make the compiler do this by specifying
    the c option with the compiler
  • CC c simpleExample.cc
  • This will produce a file called myProgram.o,
    which the compiler will look for if you just
    compile myProgram.cc

15
Preventing multiple inclusion
  • When we include something, it copies the code
    for that file into your program.
  • If we copy the same information in twice, it will
    cause problems. The second time its included,
    it will try to define things that are already
    defined, resulting in a compile time error.
  • So, now that were making header files, we have
    to do our part in preventing this from happening.
  • Fortunately, its very easy.
  • Unfortunately, its a hack. But its a hack that
    works.

16
Preventing multiple inclusion
  • ifndef _SIMPLEEXAMPLE_H_
  • define _SIMPLEEXAMPLE_H_
  • class simpleExample
  • public
  • void setNumber(int)
  • void print()
  • int holdNumber
  • endif
  • This will prevent the multiple inclusion of
    header files, thanks to the preprocessor.

17
Access Modifiers
  • So, back to this public thing. Its an access
    modifier.
  • Data Members AND Methods can either be public or
    private (or protected, but for now well just
    talk public and private)
  • public means that anyone can directly access that
    data member or call that method.
  • private means that ONLY methods WITHIN the class
    can access that data member of function

18
Access Modifiers
  • So, lets redefine our example class correctly
  • class simpleExample
  • public
  • void setNumber(int)
  • void print()
  • private
  • int holdNumber

19
Access Modifiers
  • So now holdNumber is private. If we try to
    access it like so..
  • simpleExample se1
  • se1.holdNumber5 //COMPILE TIME ERROR
  • We now control access to the private int
    holdNumber though public access method,
    setMethod. setMethod then actually changes
    holdNumber.

20
Static members in Classes
  • Remember the static keyword? This causes a
    variable (usually in a function) to remain in
    scope even after the function is finished (and
    all auto variables would get reclaimed)
  • Well, what if we defined a static data member
    inside of a class?
  • class simpleExample
  • public
  • void setNumber(int)
  • void print()
  • private
  • static int holdNumber

21
Static members in Classes
  • This will cause all instances of that class to
    share that variable. (Think of the cookie cutter
    example. Everything created by a specific cookie
    cutter shares the variable).
  • So, lets look at an example in this example,
    we also define a public method called getNumber,
    that returns holdNumber.

22
Static members in Classes.
  • class simpleExample
  • public
  • int getNumber()
  • void setNumber(int)
  • private
  • static int holdNumber
  • simpleExample se1
  • simpleExample se2
  • simpleExample se3
  • se1.setNumber(4) //now holdNumber for ALL 3 is
    4
  • se3.setNumber(se2.getNumber()1) //now
    holdNumber for ALL 3 is 5

23
Static members
  • Im teaching these because your knowledge of C
    would be incomplete without it.
  • That being said, you should try to never use
    static data members unless you have no other
    choice.
  • They are confusing, sometimes difficult to debug,
    and can cause problems on large software systems
    (something called the static initialization
    order fiasco, among others)
Write a Comment
User Comments (0)
About PowerShow.com