Sie sind auf Seite 1von 20

Programming Concepts in C++

CT025-3-2

Beginning with C++

Topic and structure of the lesson


Beginning with C++
What is C++ Applications of C++ A simple C++ program An example with Class Structure of C++ program Creating Source file Compiling and linking
CT025-3-2-PCPP (Programming Concepts in C++) Beginning with C++ Slide 2 (of 20)

Learning Outcomes
By the end of this session, you should be able to:
create, debug and run a simple C++ program using main function create, debug and run a simple C++ program using a class

CT025-3-2-PCPP (Programming Concepts in C++)

Beginning with C++

Slide 3 (of 20)

Key terms to be used


If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams:
class header file

CT025-3-2-PCPP (Programming Concepts in C++)

Beginning with C++

Slide 4 (of 20)

What is C++?
C++
evolved from C which evolved from 2 previous languages, BCPL & B provides capabilities for object-oriented programming

C++ systems
Program-development environment Language C++ Standard Library
CT025-3-2-PCPP (Programming Concepts in C++) Beginning with C++ Slide 5 (of 20)

Applications of C++
go through six phases:
edit preprocesses compile link load execute

CT025-3-2-PCPP (Programming Concepts in C++)

Beginning with C++

Slide 6 (of 20)

Basics of a Typical C++ Environment


Editor Disk

Program is created in the editor and stored on disk. Preprocessor program processes the code. Compiler creates object code and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk

Preprocessor

Disk

Compiler

Disk

Linker

Disk
Primary Memory

Loader

Disk

Loader puts program in memory.


. . . . . .

Primary Memory

CPU

. . . . . .

CPU takes each instruction and executes it, possibly storing new data values as the program executes.
Beginning with C++ Slide 7 (of 20)

CT025-3-2-PCPP (Programming Concepts in C++)

A Simple C++ Program using main() function


1 2 3 4

#include <iostream> using namespace std; int main( ) { cout<<Every age has a language of its own\n; return 0; }
Beginning with C++ Slide 8 (of 20)

5
6 7 8

CT025-3-2-PCPP (Programming Concepts in C++)

A Simple C++ Program using main() function


preprocessor directives #include <iostream> #define N 40
pound sign (#), also called an octothorp

header files
Contains the declaration of any library functions used e.g. <cmath> - sqrt()

using Directive using namespace std;


alternatives:

CT025-3-2-PCPP (Programming Concepts in C++)

std::cout<<Every age has a language of its own\n; using std::cout;


Beginning with C++

Slide 9 (of 20)

A Simple C++ Program using main() function


main function & return data type int main( ) main( ) void main( ) Standard output & input stream cout<< ; // std output stream cin>> ; // std input stream String constant Every age has a language of its own\n
CT025-3-2-PCPP (Programming Concepts in C++) Beginning with C++ Slide 10 (of 20)

A Simple C++ Program using main() function


Escape sequence \n Whitespace
Spaces, carriage returns, linefeeds, tabs, vertical tabs and form feeds Invisible to the compiler Exceptions - #include & string constants must be written in one line

Comments
Line comments // this is a line comment Block comments /* this is a block comment */
CT025-3-2-PCPP (Programming Concepts in C++) Beginning with C++ Slide 11 (of 20)

An example with Class


#include<iostream> using namespace std; class HelloWorld { public: void display(); }; void HelloWorld::display() { cout<<"Every age has a language of its own\n"; } void main() { HelloWorld objHelloWorld; objHelloWorld.display(); }

CT025-3-2-PCPP (Programming Concepts in C++)

Beginning with C++

Slide 12 (of 20)

An example with Class


Class definition / declaration
class HelloWorld { //declaration of variables

//function definition or prototype


};

Identifiers
Variables data members Functions member functions
Beginning with C++ Slide 13 (of 20)

CT025-3-2-PCPP (Programming Concepts in C++)

An example with Class


Implementation section
void HelloWorld::display() { cout<<"Every age has a language of } Does not return any data when it completes its task., so its return type is void its own\n";

Scope resolution operator (::)


Ties the function to the class and allows every instantiated class
object to use the function name

CT025-3-2-PCPP (Programming Concepts in C++)

Beginning with C++

Slide 14 (of 20)

An example with Class


Access specifiers
public: private: (default access specifier)
Data hiding

Class instance / class object


HelloWorld objHelloWorld;

CT025-3-2-PCPP (Programming Concepts in C++)

Beginning with C++

Slide 15 (of 20)

Another example with Class


#include<iostream> #include<string> using namespace std; class GradeBook { public: void setCourseName (string name) { { { courseName = name; return courseName; } }

string getCourseName()
void displayMessage() cout<<Welcome to the grade book for\n <<getCourseName() <<!<<endl; }

private:
string courseName; };
CT025-3-2-PCPP (Programming Concepts in C++) Beginning with C++ Slide 16 (of 20)

Another example with Class


int main( ) { string nameOfCourse; GradeBook myGradeBook; cout<< Initial course name is : <<myGradeBook.getCourseName( ) <<endl; cout<< Please enter the course name : <<endl;

getline( cin, nameOfCourse);


myGradeBook.setCourseName( nameOfCourse ); cout<<endl; myGradeBook.displayMessage( ); return 0; }
CT025-3-2-PCPP (Programming Concepts in C++) Beginning with C++ Slide 17 (of 20)

Follow up assignment
1. Use the preprocessor directives to include the iostream and conio header files. Create a class called Student that will hold two public fields: one that holds credit hours and another that holds the grade point average. Create the main function and within it declare a Student object identified as oneSophmore. Also within the main function, write the statements that prompt for and allow entry of the Students data. Add the statements that display the data just entered.

2.

3.

CT025-3-2-PCPP (Programming Concepts in C++)

Beginning with C++

Slide 18 (of 20)

Summary of key points


Beginning with C++
What is C++ Applications of C++ A simple C++ program An example with Class Structure of C++ program Creating Source file Compiling and linking
CT025-3-2-PCPP (Programming Concepts in C++) Beginning with C++ Slide 19 (of 20)

Highlight of next session


Tokens, expressions and Control Structures
Keywords Identifiers Basic Data Types User Defined Data Types Derived Data Types Type Compatibility
CT025-3-2-PCPP (Programming Concepts in C++) Beginning with C++

Slide 20 (of 20)

Das könnte Ihnen auch gefallen