Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Introduction To C++ Programming

Uploaded by

Quojo Berry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Introduction To C++ Programming

Uploaded by

Quojo Berry
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

INTRODUCTION TO C++ PROGRAMMING

OBJECTIVES
After studying this unit, students would be able
to appreciate the following:
• Fundamental Concepts of C++ Programming
• Install CodeBlocks application software
• Identify basic features of CodeBlocks
• Demonstrate basic coding skills in CodeBlocks

1
INTRODUCTION

C++ ranks 4th in popularity according to


2016 IEEE spectrum Top Programming
Language ranking.
“C++ is a statically-typed, free-form,
(usually) compiled, multi-paradigm,
intermediate-level general-purpose middle-
level programming language.”
2
INTRODUCTION

In simple terms, C++ is a sophisticated,


efficient and a general-purpose
programming language based on C.

It was developed by Bjarne Stroustrup in


1979.

3
INTRODUCTION

Many of today’s operating systems,


system drivers, browsers and games use
C++ as their core language.

This makes C++ one of the most popular


languages today.

4
Why learn C++?

• This is one of those questions you need to


ask before starting any programming
language.
• It helps you understand the scope of the
language, the real world usability and
how far you can get with it in terms of
support.
5
5 Reasons for Learning C++.

• C++ is irreplaceable
• You learn the internal architecture of a
computer
• Over 600,000 C++ repositories on GitHub
• 60% Stack Overflow Answer rate and
active community
• C++ job opportunities and salary
6
C++ is Irreplaceable

• With the use of C++ in development of


modern games, operating systems,
browsers, and much more, it is safe to
say that C++ is irreplaceable.

7
Major Applications of C++

Many major applications like


– Adobe Products like Photoshop, Illustrator,
InDesign
– Amazon - one of the biggest e-commerce
sites
– Autodesk products for Computer Aided
Design
– Facebook - social networking site are heavy
C++ centric products
8
You Learn the Internal Architecture of a
Computer
Since, C++ is a middle level language, you will
write code that interacts directly with the
internal hardware of the computer.
You’ll learn how the computer memory really
works, how information is stored in them,
how you can retrieve them and so on.
It is sure to expand your knowledge on the
architecture of the computer.
9
Over 600,000 C++ repositories on Github

Github, the leading open source collaboration


platform, has over 600,000 repositories for C++
alone.
This metric itself proves the worth of C++ in
the open source community as well.
Be it gaming, graphics, windows applications,
you can find tons of great open source projects
extensively used today. And, you can always
create your own.
10
60% Stack Overflow Answer Rate and Active
Community
• Likewise, with over 400,000 C++ questions
asked on StackOverflow, the number one
Q&A platform for developers, more than 60%
questions have been answered.
The number of questions asked and the
percentage of them answered shows the
interest and active support for C++ today.
So, you can expect many great developers to
help you solve real-life problems using C++.
11
C++ job opportunities and salary

• C++ developers can expect an average of


yearly $100,000 salary with over 7,700
jobs advertised every month.

The requirement of jobs comes mostly


from game development, rendering
engines and the windows applications.

12
Things to Know Before you Code in C++

Four (4) important things to know:


• C++ cannot be learnt in a day
• Learning C++ can be hard
• No, you don’t need to learn C before C++
• Don’t wait for the next C++ release

13
Compile and run C++ programming on OS

• C++ is completely free and readily


available on all platforms.
• There are multiple compilers and text
editors you can use to run C++
programming.
• These may differ from system to system.

14
Compile and run C++ programming on OS

• Run C++ program Online


• Run C++ Programming in Mac OS X
• Run C++ programming on Linux
• Run C++ in Windows (XP, 7, 8 and 10)

15
Code::Blocks Installation & Configuration

• Open the Code::Blocks Setup file and follow


the instructions (Next > I agree > Next >
Install)

• Open Code::Blocks and go to File > New >


Empty file (Shortcut: Ctrl + Shift + N)

16
Code::Blocks Installation & Configuration

• Write the C++ code and save the file with .cpp
extension.
• To save the file, go to File > Save (Shortcut:
Ctrl + S).
• Important Note: The filename should end with
.cpp extension, like: hello.cpp, your-program-
name.cpp

17
Code::Blocks Installation & Configuration

• To run the program, go to Build > Build and


Run (Shortcut: F9). This will build the
executable file and run it.

18
Code::Blocks Installation & Configuration

• If your program doesn’t run and if you see


error message "can't find compiler executable
in your search path (GNU GCC compiler)", go
to Settings > Compiler > Toolchain
executables and click Auto-detect. This should
solve the issue in most cases.

19
C++ program

• Now you have installed the compiler


based on your OS, it’s time to write your
first C++ program.

• Your first C++ program will be a “Hello


World!” program.

20
C++ program

#include <iostream>
using namespace std;
int main()
{
cout << "Hello Class BCE/BTE, Welcome to
our first C++ Programming Lesson" << endl;
return 0;
}
21
C++ program

• The program will prints as shown in the


output screen below.

22
How The Program Works

• The code is divided into six major parts:

23
What is #include <iostream>?

• iostream is what you call the header file.


It is a standard C++ input/output library
file.
• It comes packaged with the compiler/IDE
and contain mechanisms to get the
information from the user and print same
or added information to a file, screen or
any other media.
24
What is #include?

• The #include iostream file, into the


program.

• This ensures that now you’re able to use


the operations, iostream operations (like:
taking input from user, displaying
output on the screen), in the program.

25
What is using namespace std;?

• The statement is intuitive in itself, you are


“using” the “namespace” “std” in your
file.

• We use the namespace std to make it


easier to reference operations included in
that namespace.

26
What is using namespace std;?

• If we hadn’t used the namespace, we’d


have written std::cout instead of cout.

• This tells the compiler that every cout is


actually std::cout.

27
What’s a namespace?

• It’s a region where your code resides.

• It limits or expands the scope of your


code to one or more files.

28
Why do you use namespace?

• Like two persons can have the same


name, variables and functions in C++ can
have same names as well.

• The use of namespace is to avoid the


confusion of which variables/functions
you are referencing to.

29
What is std?

• std is a standard namespace used in C++.

Semicolon “;”
• The semicolon is a terminal. It terminates
a statement.
• When missed or incorrectly used, it will
cause a lot of issues.
30
int main() { }

• As the name suggests, it is the main function


of the program.
• The code inside { } is called the body and is
executed first when you run your C++
program.

• It is one code that is mandatory in a C++


program. If you just have this line of code
alone, your program will be valid.
31
cout << “Hello World!”;

• This statement prints “Hello World!” or “any


other statement” onto the output screen.
The cout is an object of standard output
stream.
• What this means is, it outputs/prints the data
after << , i.e. Hello World! into a stream (in
this case, the output screen).

32
What is a stream?

• Stream is basically a sequence of objects,


usually bytes.

• It can describe files, input/output


terminal, sockets, etc.

33
What is <<?

• What is <<

<< is the insertion operator used to write


formatted data into the stream.

34
What is return 0;?

• This statement returns 0 ‘zero’.

• This is called a return statement. It isn’t


mandatory to return anything from the
main() function but is rather a convention.

• If not return, the compiler returns a status


automatically.

35
Why zero in return statement?

• It denotes Exit status of the application


that basically tells system “The program
worked fine.”

36
Guidelines in C++ Programming

As you study on C++, you’ll gradually understand


there is no one way to get something done.
• Learning by doing
• Follow C++ standards
• Read other’s C++ codes
• Break things
• Join C++ communities

37
Learning by doing

• Practice will make you a better programmer.


• Don’t just copy code and run it. Take some
time to think what the code actually does.
• Replicate it on your system and see what
errors occur and most importantly, learn from
them.
• Only then will you better yourself as a
developer.
38
Follow C++ standards

• It is really important to follow a good C++


standard when you start programming.
• This provides a set of easy rules to follow for a
particular purpose in a particular setting.

• Take a look at IsoCPP’s coding standard FAQ


and also follow the C++ Core Guidelines.

39
Read other’s C++ code

• Join GitHub open source projects and read


others code. This can be overwhelming at first
when you see all the code in the project.
• You can use Code Whittling to start small and
only focus on one thing at a moment.
• You’ll not only learn other’s style of coding but
you’ll also understand how they think.

40
Break things

• Don’t be afraid to break things the way it is.


You’ll be amazed to find how much you can
learn from the broken code filled with errors.
• Errors are developers best friend. Understand
what the error is about.
• Follow the error trail that takes you to the root
of the issue, fix them and learn from them.

41
Join C++ communities

• Get help from others. There are tons of


great C++ communities that will help you
solve real life problems and most
importantly, become a better developer.

42
Join C++ communities

• Some C++ communities are:


– StackOverflow - Most Popular programming Q&A
site on the web
– Codechef - Practice questions, challenges and a
large community of programmers
– CodeProject - For those who code, with in-depth
articles and huge community of coders

43
Conclusion

• C++ is a great language to start your


programming journey with.
• The experience will take you a long way
in becoming a great developer.
• So why wait. Get started by practicing
and visiting any of the tutorial websites.

44
Coding Exercise One (1)

Write a C++ Program to Display Number


(Entered by the User)

#include <iostream>
using namespace std;

int main()
{
int number;

cout << "Enter an integer :: ";


cin >> number;

cout << "\nThe Number entered is :: " << number<<"\n";


return 0;
}
45
Coding Exercise One (1)
Output Code

46
Coding Exercise One (1)
Output Screen

47
QUESTIONS TIME

48
NEXT TOPIC

• Data Definition Structures

49
Reading Assignment
• Write a C++ Program to Add Two Numbers

50
References:
1. https://www.programiz.com/article/algorithm-programming
2. The C++ Programming Language (4th Edition)
3. Programming -- Principles and Practice Using C++ (2nd Edition)

51

You might also like