C++ Programming COSC 1305
Popular in Course
Popular in Chemistry
This 12 page Class Notes was uploaded by Lowell Harris on Saturday September 19, 2015. The Class Notes belongs to COSC 1305 at University of Houston taught by Staff in Fall. Since its upload, it has received 71 views. For similar materials see /class/208184/cosc-1305-university-of-houston in Chemistry at University of Houston.
Reviews for C++ Programming
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/19/15
Lecture 1 C Basics Dragan Mirkovic DepannmntofCompumrScbnce UnNemHyofHou on D Mirkovic CH Programming Spring 2005 Announcements Today C Basics Ch 1 In Inine Introducing C Basic terminology Classes and objects Standardization of C CC differences D Mirkovic CH Programming Spring 2005 Introduction C is one ofthe most popular programming languages today Industrialstrength language Incorporates the efficiency of C with 00 features Wellsuited for development of large complex software systems and libraries Strongly typed language Compiler performs strict type checking on variables and expressions Objectoriented 00 approach Focus on objects that make up an application problem Program structure relationship between objects Processoriented approach Program is organized as a hierarchy of tasks procedures Procedural programming languages C Fortran Pascal D Mirkovic CH Programming Spring 2005 History of C C was developed at Bell Labs around19691973 for implementation ofthe UNIX operating system by Dennis Ritchie 90 of UNIX was then written in C 1989 ANSI Standard for C C was written by Bjarne Sroustrup at Bell Labs during 1983 1985 C is an extension of C Prior to 1983 Bjarne Stroustrup added features to C and formed what he called quotC with Classesquot He had combined the use of classes and objectoriented features with the power and efficiency of C The term C was first used in 1983 D Mirkovic CH Programming Spring 2005 Objects Individual entities that comprise an application Examples 7 student A college student object 7 Course A single course taken by a student Object characteristics Attributes contents of an object Operations functions of an object Object encapsulates both attributes and operations Example A student object may have Name and ID as attributes and Display and CalculateGrade as operations D Mirkovic CH Programming Spring 2005 Classes Description ofthe characteristics shared by all objects of the Similar to Abstract data type ADT in C inheritance polymorphisms and much more Object An instance of the class Example 222334444 Class student Jones long i ch39a39r las t39Na39m39e 30 ch a r firstName 3 0 E Sam t t tC d39t 39 1 39 quotFe 139 5 232114567 VDld InpthE AI 39void Outputov lga float Clcul ateGrade 39 Pnce 39 52 D Mirkovic CH Programming Spring 2005 Inheritance Different objects can have some common attributes and operations In some cases it is useful to define a base class which contains the common characteristics Corresponds to Is a relationship in Entity relationship data model Example Base class Derived classes Airplane D Mirkovic CH Programming Spring 2005 Polymorphism A single name can denote objects ofdifferent types The actual type of the object doesn t have to be known at compile time Most programming languages do not support polymorphism Unions in C allow for a speci c form of polymorphism In C polymorphic objects must be instances of classes related by inheritance Example calcSpeedO function for Vehicle Car Truck Airplane family Dynamic binding vs static binding D Mirkovic CH Programming Spring 2005 Structure of a C program o ExamPIe Example my first program in c Equivalent program in C include ltstdiohgt include ltiostreamhgt int main 0 int main printf He110 Wor1dn 7 cout ltlt quotHello Worldquot return 0quot return 0 ostructure Comments line comment block comment Preprocessor directives the main function declaration standard output steam cout equivalent to stdout D Mirkovic CH Programming Spring 2005 Variables data types constants Variable a portion of memory to store a determined value Each variable needs an identifier that distinguishes it from the others a5 b2 aa1 resu1ta b Identifier a sequence of one or more letters digits or underline symbols The length of an identifier is not limited Variable identifiers should always begin with a letter or an underline character they cannot match any key word D Mirkovic CH Programming Spring 2005 Key words in C ANSIC standard keywords asm auto bool break case catch char class const constcast continue default delete do double dynamiccast else enum explicit extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpretcast return short signed sizeof static staticcast struct switch template this throw true try typedef typeid typename union unsigned using virtual void volatile wchart There may be additional reserved words depending on the compiler and operating system The C language is quotcase sensitivequot D Mirkovic CH Programming Spring 2005 Data types Type Description Bytes Range char character or integer 8 bits length 1 signed 7128 to 127 unsigned 0 to 255 short integer 16 bits leng h 2 Signed 732768 t0 32767 unsigned 0 to 65535 int integer 32 bits leng h 4 signed 72147483648 to 2147483647 unsigned 0 to 4294967 95 long integer 6432 bits length 84 Signed 7214748364810 2147483647 unsigned 0 to 4294967295 float Single precision oating point 4 3 4e r 38 7 digits number double Double precision oating point 8 1 79 t 308 i5 digil number long Extended precision oating point 10 1 2e H4932 19 digits double number boul Boolean value true or false 1 true Orfalse wchart Wide character N 0 to 65535 D Mirkovic CH Programming Spring 2005 Declaration of variables In order to use a variable in C we must rst declare it The syntax in a float mynumber Initialization ofvariables type 39d tifie s vslue 7 type identifier ini islvslue 7 Examples int a0 include ltiostresnhgt int main l declaring Variables int s 1 int result process 5 a b 2 a a 1 result print out result cout ltlt result teminate the program return 0 l D erkovlc 04 Programming Spring 2005 Scope of variables In C we can declare variables anywhere in the source code not in C include ltinsuesnngt ensr suing zn unsigned int Ilumhex fsnns unsigned snm nge flunk mlumhel nunlhex ne cunt ltlt nlex you 399 cin gtgt nge Gobal variables Loca variables External variables D erkovlc 04 Programming Spring 2005 Constants Literals Defined constants Integer floats characters dem e lam14159255 and strings define NEWLINE 39n Examples define WIDTH 100 Integer Declared constants 75 decmal V th the const prefix you can 0113 natal declare constants with a specific 0x413 hexadecimal type exactly as you would do with a variable const int width 100 const char tab 39t39 Floating point numbers 314159 314159 602e23 602 x 1023 Character and strings v quotHello worldquot D Mirkovic CH Programming Spring 2005 Operators Assignment Arithmetic operators Compound arithmetic operators gtgt ltlt r r r o amp IncrementDecrement operators 77 Relational operators gt lt gt lt According to the ANSIC standard the result is a bool value true or false Different from C int Logic operators u Conditional operator 39 Bitwise Operators amp I A ltlt gtgt Explicit type casting Operators type or type D Mirkovic CH Programming Spring 2005 Storage Duration All named objects in C have a certain lifetime determined by its storage class Automatic storage duration default Object is destroyed after exiting the block 7 auto keyword Static storage duration The object has the same lifetime as the porgram 7 static keyword Dynamic storage duration 7 new and delete keywords D Mirkovic CH Programming Spring 2005 CC Differences New keywords Single line comments Variable declaration Only at the beginning of their block in C Anywhere in the block priorto their 1St use Cast operator 2 ways in C Function prototypes are required TypeSafe Linkage Named constants in C D Mirkovic CH Programming Spring 2005 Reference Parameters Passing arguments by Passing arguments by reference in C reference in C void swapint ampx int ampy void swapint x int y int tmp x int tmp x X Y x y y tmp y tmp int A 20 B 10 B ampB printfquot od dnquot A B int A 20 swapampA 10 SWBP A B y39 cout ltltAltlt 3939 ltlt B D Mirkovic CH Programming Spring 2005 Stream IIO Stream Output The stream output operator ltlt appends an expression to the output stream Examples cout ltlt 10 pn39nls number 70 on sateen cout ltlt quotHelloquot pn39nls Hello on sateen cout ltlt x pn39nls the some of x variable on sateen cout ltlt quotHello I am quot ltlt age ltlt quot years 0111quot When writing an expression use 0 to force evaluation of the expression before printing cout ltlt 10 x Use a newline character n39 to force line breaks cout ltlt 10 x ltlt n Use endl stream manipulator a newline and flush the output buffer cout ltlt 10 x ltlt n D Mirkovic CH Programming Spring 2005 Stream IIO Stream Input The stream input operator gtgt extracts data from an input stream Examples int n read from ream By default operator skips whitespace tabs spaces newlines int a 13 float r char 6 cin gtgt a gtgt b gtgt r gtgt ch 7 cinget canbeusedfor character input Example include ltiostreamhgt char name80 ch 39039 int i 0 cout ltlt quotEnter your name whilel cingetch ifch39n break namei ch namei 39039 D Mirkovic CH Programming Spring 2005 Summary C is an industrialstrength language Incorporates the ef ciency of C with 00 features Wellsuited for development of large complex software systems and libraries Strongly typed language Basic terminology Objects Classes Inheritance Polymorphism Differences between C and C Stream lO D Mirkovic CH Programming Spring 2005 Homework Exercises 14 page 20 in text D Mirkovic CH Programming Spring 2005
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'