Note: Syntactically, a ; is needed at the end of a struct definition.



Similar documents
Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Ch 7-1. Object-Oriented Programming and Classes

C++FA 5.1 PRACTICE MID-TERM EXAM

An Incomplete C++ Primer. University of Wyoming MA 5310

Comp151. Definitions & Declarations

Computer Programming C++ Classes and Objects 15 th Lecture

Goals for This Lecture:

C++ INTERVIEW QUESTIONS

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Chapter 5 Functions. Introducing Functions

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Illustration 1: Diagram of program function and data flow

EP241 Computer Programming

CS 111 Classes I 1. Software Organization View to this point:

Appendix M: Introduction to Microsoft Visual C Express Edition

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Answers to Review Questions Chapter 7

Object Oriented Software Design II

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

C++ Programming Language

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Member Functions of the istream Class

Arrays in Java. Working with Arrays

Java Classes. GEEN163 Introduction to Computer Programming

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Java Server Pages and Java Beans

Object Oriented Software Design II

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Object-Oriented Programming

Passing 1D arrays to functions.

COSC 181 Foundations of Computer Programming. Class 6

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

The C Programming Language course syllabus associate level

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

The separation principle : a principle for programming language design

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

Curriculum Map. Discipline: Computer Science Course: C++

C++ Overloading, Constructors, Assignment operator

Data Structures using OOP C++ Lecture 1

AP Computer Science Java Subset

CISC 181 Project 3 Designing Classes for Bank Accounts

Appendix K Introduction to Microsoft Visual C++ 6.0

Coding conventions and C++-style

Advanced Data Structures

Introduction to Programming Block Tutorial C/C++

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

Konzepte objektorientierter Programmierung

Common Beginner C++ Programming Mistakes

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

13 Classes & Objects with Constructors/Destructors

Functions and Parameter Passing

Class 16: Function Parameters and Polymorphism

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Class Interfaces, Design, and Implementation

5 Arrays and Pointers

N3458: Simple Database Integration in C++11

How To Write Portable Programs In C

Semantic Analysis: Types and Type Checking

Object Oriented Software Design II

J a v a Quiz (Unit 3, Test 0 Practice)

C# programming. Introduction. By Per Laursen

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

C++ Programming Tutorial Part II: Object-Oriented Programming. C. David Sherrill Georgia Institute of Technology

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Chapter One Introduction to Programming

Moving from CS 61A Scheme to CS 61B Java

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Visual Studio 2008 Express Editions

Glossary of Object Oriented Terms

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

: provid.ir

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Darshan Institute of Engineering & Technology PL_SQL

3 Representation in C++.

Tutorial on C Language Programming

Cooperative Learning Method Based On Game Design and Visual Object Oriented Environment to Teach Object Oriented Programming Course

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Solving Problems Recursively

C++ Input/Output: Streams

Applied Informatics C++ Coding Style Guide

Introduction to Java. CS 3: Computer Programming in Java

Visual Basic Programming. An Introduction

Course Title: Software Development

Schedule. Structures and Classes in C++ Outline. Goals for This Topic. Another Example of a Structure. What is a Structure? Classes May 12-17, 2005

CS107L Handout 02 Autumn 2007 October 5, 2007 Copy Constructor and operator=

Basics of I/O Streams and File I/O

Transcription:

Structs, Classes, and Arrays Structs A type representation containing a set of heterogeneous members with possibly varying types. struct Student { string name; int id; double tuition; ; Note: Syntactically, a ; is needed at the end of a struct definition. Accessing a member of a struct, you can use the. operator (since members of a struct by default are public. int main() { Student s; s.name = Richert ; s.id = 12345678; s.tuition = 0; cout << Name: << s.name <<, id: << s.id <<, tuition: << s.tuition << endl; return 0; Memory Organization of structs - Members are stored contiguously and space is allocated based on the types of members Student s; cout << sizeof(s.name) <<, << sizeof(s.id) <<, << sizeof(s.tuition) << endl; Padding - Some members, such as chars, may consume more memory than the actual size of the type. - Mainly for performance reasons and how data is fetched. - Depends on the compiler and underlying computer architecture. Classes A class is a representation of a user- defined type. Classes consist of: - An interface: What operations are available when using this class. - An implementation: The definition of how things are done.

Example of a class interface: #include<iostream> using namespace std; class Student { public: Student(); // constructor Student(string studentname, int studentid, double studenttuition); // constructor overloading Student(Student& student); // copy constructor string getname(); // accessor int getid(); // accessor double gettuition(); // accessor void setname(string StudentName); // mutator void setid(int studentid); // mutator void settuition(double studenttuition); // mutator private: string name; int id; double tuition; ; Public vs Private - The variables and functions declared as private cannot be referenced anywhere except within the class functions. - The variables and functions declared as public can be referenced anywhere. Abstract Data Types (ADTs): - A data type where the programmers who use this class do not have access to the details of how the values and operations are implemented. - Also known as data hiding, data abstraction, and encapsulation. - Typically, a consumer of the class needs to be aware of all the public fields. Scope Resolution Operator :: - When a member function is defined, the definition must include the class name because there may be two or more classes that have member functions with the same name. - Without it, the compiler does not know which class member function the definition is for. Examples: string Student::getName() { return name; int Student::getId() { return id; double Student::getTuition() { return tuition; void Student::setName(string studentname) { name = studentname; void Student::setId(int studentid) { id = studentid; void Student::setTuition(double studenttuition) {

Accessor and Mutator Functions - A function that simply returns private members values are called accessor functions (i.e. they access the data) - getname(), getid(), gettuition() are examples of accessor functions. - A function that simply sets private members values are called mutator functions (i.e. they change the data). - setname(), setid(), settuition() are examples of mutator functions. Structures vs Classes - In general, a C++ struct can do anything a C++ class can do! - including having functions, inheritance, public / private members - Default behavior is different: o If public or private is not defined, structs default to public and classes default to private. Constructors - A special kind of function with the same class name that it s defined in. - A constructor is called when an object of that class is declared. - Constructors are the only functions that do not have a return type. - The only time you will use a constructor is during declaration. - Default constructors can be defined by you (which is good style). - If a default constructor is not defined, the compiler will generate one if no other constructor is defined! Otherwise it will not. // Example of default constructor Student::Student() { name = John Doe ; id = 0; tuition = 0.0; // Example of constructor overloading Student::Student(string studentname, int studentid, double studenttuition) { name = studentname; id = studentid; Copy Constructor - Take in an existing Object in a constructor's parameter and set all of its fields to the fields of the object, thus copying one object's fields to the current object. - Copy constructors must pass its parameter by reference Pass- by- value vs. Pass- by- reference - Also know as call- by- value and call- by- reference. - Pass- by- value copies the parameter value into a variable that is local to the function.

- Pass- by- reference modifies the value in memory and does not create a local copy. // Example of copy constructor Student::Student(Student& student) { id = student.getid(); name = student.getname(); tuition = student.gettuition(); Arrays - Used to process a collection of data of the same type. Statically- allocated arrays int a[10]; - The array will have a size of 10 * sizeof(int). - Be careful! Arrays do not know what their size is! Bracket Syntax - Accessing an individual cell of an array uses a traditional [ ] syntax, which is indexed starting at 0. - For all practical purposes, a specific element in an array can be treated as a type that the array was defined as. Passing Arrays as parameters You can pass arrays in parameters as you would expect, but there are some things to take into consideration: 1. The value that is actually passed into the function is the address of the 0 th index of the array. 2. Arrays do not inherently have a size associated with it (i.e..size() or.length()), therefore you should pass the size of the array as a separate parameter. void printarray(char grades[], int size) { for (int i = 0; i < size; i++) { cout << a[ << i << ] = << a[i] << endl; Multi- dimensional Arrays int grid[4][6]; // 2-D 4x6 array (or 4 1-Dimensional arrays of size 6)

Example of initialization / looping through a 3- D array: int grid[2][3][2] = {{0,1, {2,3, {4,5, {{6,7,{8,9,{10,11; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { for (int k = 2; k < 2; k++) { cout << grid[i][j][k] <<, ; Passing multidimensional arrays as parameters in functions - When passing in a multi- dimensional array, the size of each dimension (except the first) must be defined in the parameter. - Note: it s legal to pass in the 1 st dimension s size, but it s not necessary since the compiler ignores it. void somefunction(int grid[][6], int size) { Example Organization of breaking the Student Class into multiple files: // student.h #ifndef STUDENT_H #define STUDENT_H #include<string> class Student { public: Student(); Student(std::string studentname, int id, double tuition); Student(Student& student); std::string getname(); int getid(); double gettuition(); void setname(std::string studentname); void setid(int studentid); void settuition(double studenttuition); private: std::string name; int id; double tuition; ; #endif - - - - - - - - - - - - - //student.cpp #include "student.h" #include<string> using namespace std;

Student::Student() { name = "John Doe"; id = 0; tuition = 0; Student::Student(string studentname, int studentid, double studenttuition) { name = studentname; id = studentid; Student::Student(Student& student) { id = student.getid(); name = student.getname(); tuition = student.gettuition(); string Student::getName() { return name; int Student::getId() { return id; double Student::getTuition() { return tuition; void Student::setName(string studentname) { name = studentname; void Student::setId(int studentid) { id = studentid; void Student::setTuition(double studenttuition) { - - - - - - - - - - - - - // main.cpp #include<iostream> #include<string> #include"student.h" using namespace std; int main() { Student me1("richert", 12345678, 0.5); Student me = Student(me1); cout << "Name: " << me.getname() << endl; cout << "Id: " << me.getid() << endl; cout << "Tuition: " << me.gettuition() << endl; return 0; Note: You should not use using namespace in your header files. This can have unintended side- effects for consumers of this header that are not expecting to use a namespace.