1 / 30

C++ Programming Concepts

C++ Programming Concepts. Lecture 3 Pointers in C/C++. Introduction. Basic Concepts “Ordinary” variables Declaring, pointing and dereferencing. Passing by value Classes and Objects Structures Array Linked List. Some Basic Concepts - 1.

enya
Download Presentation

C++ Programming Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Programming Concepts Lecture 3 Pointers in C/C++

  2. Introduction • Basic Concepts • “Ordinary” variables • Declaring, pointing and dereferencing. • Passing by value • Classes and Objects • Structures • Array • Linked List

  3. Some Basic Concepts - 1 • Should all be familiar with “ordinary” variables declarations. • Such as…. • int nCount; • double dBalance; • char chLetter; • char arName[50]; • and so on…..

  4. Basic Concepts - 2 • Should also be familiar with assignments. • Such as ….. • nCount = 5; • dBalance = 12.36; • chLetter = ‘Z’; (note single quotes); • arName = “David D. Hodgkiss”; ?????? • This is wrong – the compiler will complain • Discuss this later.

  5. Declare and Assign • Should also be familiar with….. • int nCount = 4; • double dBalance = 12.36; • char cLetter = ‘Z’; • char arName[] = “David D. Hodgkiss”;

  6. Why pointers? Consider this code (abridged) int main(void) { a = 1; b = 2; swap(a, b); cout << a << b << endl; } void swap(int a, int b) { int temp; temp = a; a = b; b = temp; } When ‘a’ and ‘b’ printed after swap result will be…. a = 1; b = 2; Why?

  7. Passing values. • C does not pass the variable to functions. • It passes the value that the variable holds. • Within the function new variables are created. • This is known as “Pass by Value” • In “swap” – it is only the variables within swap() that have their values swapped. • Variables lost when function returns.

  8. How can we swap? • Using pointers. • Pointers • Are themselves variables • That hold a memory address • instead of a variable value • Declaration • int *pInt;

  9. Reading the declaration • int *pInt; • Break the declaration down • “pInt” The pointer’s name • “*” Indicating it is a pointer • “int” Type of pointer • So…. • “pInt” is a pointer to an integer

  10. Reading the declaration • int *pInt; • Break the declaration down • “pInt” The pointers name • “*” Indicating it is a pointer • “int” Type of pointer • So…. • “pInt” is a pointer to an integer

  11. Other declaration examples • double *pDouble; • char *pChar; • int *pMyPointer; • long *pYourPointer; • and so on

  12. Using pointers • The pointer variable holds an address location. • From whence do’th this address derive? • Consider…. int nCount; // an integer variable int *pInt; // a pointer to an integer Requirement – make pInt “point” at “nCount”

  13. Finding the address • We can use the “&” character (address of) • So…. • pInt = & nCount; // spaces added for clarity • pInt equals the address of nCount

  14. Getting at the value • How do we get a value via the pointer? • Using the ‘*’ character again. • To “dereference” the pointer. cout << *pInt << endl; output the value at which “pInt” is pointing.

  15. More accessing • myInt = *pInt; • arInts[5] = *pInt; • *pInt = nCount; • *pInt = 6; • *pInt = arInts[12]; • *pA = *pB; //is this the same as pA = pB? • You should experiment with these concepts • myInt = nInt; // What would happen here?

  16. Back to swap • How can me make swap work correctly? • By using pointers! • Instead of swap receiving variable values • Pass pointer values to it.

  17. Swapping with pointers • swap(&a, &b); // this is in “main” • void swap(int *pA, int *pB){ int temp; temp = *pA; *pA = *pB; *pB = temp;}

  18. What about classes • Consider that we have a class called “Account” • Could instantiate using • class Account Fred; • Could also use pointers • First create a pointer to a class of type Account • class Account *pAcc; • Now instantiate • pAcc = new Account;

  19. pAcc = new Account; • “pAcc” is a pointer • “new” is a C++ keyword • It allocates a block of memory • and • passes (returns) the location (address) of that block (containing an Account Object) to pAcc • So • pAcc is pointing at the Account object. • what if “new” fails to allocate?

  20. Interacting with the object • When using a pointer we do not use the “dot” notation. • We use an arrow “->” • That consists of …. • a “dash” • followed by a • “greater than” symbol

  21. Interacting with the object • Remember pAcc = new Account; • To interact use…… • pAcc->SetBalance(12.36); • pAcc->SetIntRate(3.4); • dBal = pAcc->GetBalance(); • dInt = pAcc->GetIntRate();

  22. Big deal – what good is it • Let’s have an array • of pointers • Account *arAccounts[10]; • An array called “arAccounts” • Has 10 elements • Each of which is…… • A pointer to…… • An object of type Account

  23. Handling objects via an array • Some code ….. Account *arAccounts[10]; for(nCount = 0; nCount < 10; nCount++) arAccounts[nCount] = new Account; • That will create an array of pointers to ten separate Account objects.

  24. Accessing via an array • arAccounts[1]->SetBalance(12.36); • arAccounts[1]->SetIntRate(5.36); • dBal = arAccounts[1]->GetBalance(): • dInt = arAccounts[1]->GetIntRate(); • By using an array we need not find names for each instantiation.

  25. What about strings • char arName[50]; • The actual array name is a pointer. • To copy one “string” to another. • E.g. contents of arName[] to arCustomer[] • ?? arCustomer = arName • ?? arCustomer[] = arName[] Neither of these will work

  26. Copying and manipulating strings • C provides a number of functions to copy strings • Copy strcpy(……..) • Concatenate strcat(…….) • Compare strcmp(……) • Length strlen(……)All of these use pointers.

  27. C++ & Strings • C++ can handle strings as objects • We will be looking at string handling classes in a later lecture. • It hides the implementation!

  28. prtObject prtObject prtObject ptrNext ptrNext ptrNext Linked Lists • Excellent for handling an unknown number of objects. ptrHead Account Object Account Object Account Object ???

  29. Further investigation • “this” is a pointer • To what? • Background reading • Test by developing some code

  30. Summary • Some basic concepts • Apply to pointer declaration • Finding the address of a variable • Dereferencing the pointer • Passing by reference – using pointers • Handling objects • Arrays • Linked Lists

More Related