Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Preprocessor Directives and Object Oriented Programming | CSE A205, Study notes of Engineering

Material Type: Notes; Class: Introduction to C Programming for Engineers; Subject: Computer Systems Engineering ; University: University of Alaska - Anchorage; Term: Spring 2009;

Typology: Study notes

2009/2010

Uploaded on 03/28/2010

koofers-user-2se
koofers-user-2se 🇺🇸

3.5

(2)

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE294A
Introduction to C Programming for Engineers
Lecture #24
Jeffrey Miller, Ph.D.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Preprocessor Directives and Object Oriented Programming | CSE A205 and more Study notes Engineering in PDF only on Docsity!

CSE294A

Introduction to C Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

Preprocessor Directives

Preprocessing occurs before a program iscompiled

Preprocessor directives begin with a

Only whitespace characters and comments mayappear before a preprocessor directive on a line

#define Preprocessor Directive

The #define preprocessor directive is used to createsymbolic constants

#define identifier replacement-text

When a #define is used, all subsequent occurrences of theidentifier that appear outside of string literals are replacedby the replacement-text automatically before the programis compiled

The values of variables are not evaluated in preprocessordirectives because they are evaluated prior to the codebeing compiled

#define PI 3.

#define Proprocessor Directive

The #define preprocessor directive can be used to definemacros

A macro is similar to a function, but the macro will beexpanded in-line in the program when it is called #define PI 3.14159 #define

CIRCLE_AREA(x)

(PI)

(x)

(x)) ... area = CIRCLE_AREA(4); // expanded to: area

Conditional Preprocessor Directives

Conditional preprocessor directives enable you to control the execution ofother preprocessor directives and the compilation of program code - #ifdef is used to determine if a preprocessor directive has been defined - #ifndef is used to determine if a preprocesor directive has not been defined - #endif is used to denote the end of the conditional preprocessor directive - NOTE: This is often used when including header files that have classes defined, which issomething you will learn in the object-oriented programming class next semester #ifndef DEBUG #define DEBUG - #endif

Object-Oriented Programming

C is not an object-oriented language, though C++ is - C++ is a superset of C, meaning that anything you cando in C you can also do in C++

But C++ can do more!

In C, the closest construct we have to object-orientation is structures, which logically groupvariables together - In C++, a object (which is an instance of a class) hasboth variables and functions

Class Implementation

// in a file called Bank_Account.cpp #include “Bank_Account.h” Bank_Account::Bank_Account(double bal, int acct, char* name) { balance = bal; account_number = acct; strcpy(customer_name, name); } void Bank_Account::deposit(double amount) { balance += amount; } void Bank_Account::withdraw(double amount) { balance -= amount; } double Bank_Account::check_balance() { return balance; }

Using a Class

#include “Bank_Account.h” void main() { Bank_Account ba1(2000.30, 1234, “jeff”); Bank_Account ba2(3000.40, 2345, “mike”); ba1.deposit(1000.00); ba2.withdraw(2000.00); printf (“ba1 balance = %.2lf\n”, ba1.check_balance()); printf (“ba2 balance = %.2lf\n”, ba2.check_balance()); }

Homework

Homework #8 is posted!