c++ fundamentals, conditionals, loops, functions - tutorial 8 · c++ fundamentals, conditionals,...
TRANSCRIPT
C++ Fundamentals, Conditionals, Loops,
FunctionsTutorial 8
Akhil Krishnan
Department of Computing and SoftwareMcMaster University
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 1 / 28
Outline
1 Getting EclipseInstalling EclipseA Simple Program
2 ExerciseSolution
3 Java to C++ TransitionArithmetic OperationsInput OutputStringsFilesArraysObjects Creation
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 2 / 28
Getting Eclipse Installing Eclipse
Install Eclipse for C++
Download link:https://eclipse.org/downloads/
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 3 / 28
Getting Eclipse Installing Eclipse
Install Eclipse for C++
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 4 / 28
Getting Eclipse Installing Eclipse
Install Eclipse for C++
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 5 / 28
Getting Eclipse Installing Eclipse
Installing Eclipse on a Mac
Mac
1 Find .tar.gz file (probably in your Downloads folder)2 Drag it to the Desktop3 Double-click on it and find a folder named “eclipse”4 Drag “eclipse” folder into Applications folder5 Inside “eclipse” folder find application named “Eclipse”6 Drag it to your dock to make it easier to launch
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 6 / 28
Getting Eclipse Installing Eclipse
Installing Eclipse on a PC
Windows
1 Find .zip file (probably in your Downloads folder)2 Right-click on it3 Select “Extract All. . . ” and extract to “C:” (or whatever your
root drive is)4 After it is extracted, drag “eclipse.exe” to your Start menu to
make it easier to launch
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 7 / 28
Getting Eclipse A Simple Program
A Simple Program
Choose the Work space location. Choose a di↵erent Work spacelocation from Java workspace so that you can use themsimultaneously.To create a New Project, select File ! New ! C++ Projectand choose the following option in the Wizard.
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 8 / 28
Getting Eclipse A Simple Program
A Simple Program
To create a new source file, select File ! New ! Source File.Then a New Window opens as shown below.
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 9 / 28
Getting Eclipse A Simple Program
A Simple Program
Type the following code and save the file. Then Build Project asshown belowThen run the program as shown below.
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 10 / 28
Getting Eclipse A Simple Program
A Simple Program
The File Header is automatically populated with details likeAuthor name, Date Created On and the name of the File
Then run the program as shown below.
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 11 / 28
Getting Eclipse A Simple Program
Preferences
Go to Eclipse ! Preferences... ! C\C++ ! New C\C++Project Wizard
In ToolChains, click on MacOSX GCC and then select ”MakeToolChains Preferred” and select OK.
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 12 / 28
Exercise Solution
Exercise: Try this ...
Write a C++ program to find prime numbers between givenrange of numbers
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 13 / 28
Exercise Solution
Solution
1 #include <iostream >
2 #include <math.h>
3 using namespace std;
4 int main()
5 {
6 int startNum ,endNum;
7 int found=0,count =0;
8 cout <<"Enter Number START of Range: ";
9 cin >>startNum;
10 cout <<"Enter Number END of Range: ";
11 cin >>endNum;
12 for(int i=startNum;i<= endNum;i++)
13 {
14 for(int j=2;j<=sqrt(i);j++)
15 {
16 if(i%j==0)
17 count ++;
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 14 / 28
Exercise Solution
Continued...
1 }
2 if(count ==0&&i!=1)
3 { found ++;
4 cout <<"Prime Number -> "<<i<<endl;
5 count =0;
6 }
7 count =0;
8 }
910 cout <<"Total Prime Number Between Range "<<startNum <<
" to "<<endNum <<" = "<<found <<endl;
11 return 1;
12 }
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 15 / 28
Exercise Solution
Output
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 16 / 28
Java to C++ Transition Arithmetic Operations
Java Vs C++: Arithmetic Operations
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 17 / 28
Java to C++ Transition Arithmetic Operations
Java Vs C++: Arithmetic Operations
Constants
To create a constant value in Java requires the notation of staticfinal before the variable declaration. Note that the constantMAX in C++ uses, the much simpler and more direct constkeyword.
Boolean
C++ does not type check the values you provide to ifstatements, while loops or for loops. You can make mistakes andC++ will not warn you in places where Java would report anerror.
Output
Note that in Java a complex line of output is assembled using +to concatenate strings together. In C++ this is done using thehh operator.
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 18 / 28
Java to C++ Transition Input Output
Java Vs C++: Input Output
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 19 / 28
Java to C++ Transition Input Output
Java Vs C++: Input Output
Importing
In Java, the easiest way to access input from the keyboard isthrough the Scanner class. This is imported at Main.java.Similarly in C++ we include the iostream file
Input source
In Java we get input from the keyboard via System.in. Weconnect this to a scanner as shown at Main.java. In C++ this issimpler. We access keyboard input from the variable cin, whichwas declared inside of iostream.
Reading input
To read in an integer in Java we used the nextInt() methodshown at Main.java. To do the same thing in C++ we use theii operator as shown at Main.cpp
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 20 / 28
Java to C++ Transition Strings
Java Vs C++: Strings
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 21 / 28
Java to C++ Transition Strings
Java Vs C++: Strings
string
To work with strings in Java we simply use the String classdirectly. The String class is automatically imported. In C++ thestring class is not capitalized and is not automatically imported.We must include its declarations as in C++.Java uses String butC++ uses string.
Relational Operators
In C++ the relational operator can be used to comparestrings.This is because in C++ strings are stored as ASCIIcharacters.
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 22 / 28
Java to C++ Transition Files
Java Vs C++: Files
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 23 / 28
Java to C++ Transition Files
Java Vs C++: Files
Include files
In C++ if we are going to read and write with files we need toinclude iostream and fstream at Main.cpp
Opening a file for output
In Java there are three steps needed to open a file for writing: 1)specifying the file name and output stream, 2) wrapping theoutput stream with a PrintStream so that the nice methods areavailable and 3) catching an exception for any errors. In C++there is only the opening of the output file and an if statementto catch any errors. In Java we specify the file name(”data.txt”) by including it in a FileOutputStream constructor.In C++ we use the open() method as at Main.cpp. If there is anerror in opening a file, Java with raise an exception. This mustbe caught in order to handle the error.
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 24 / 28
Java to C++ Transition Arrays
Java Vs C++: Arrays
Java:You can create an array by using the new operator with thefollowing syntax:
dataType[] arrayRefVar = new dataType[arraySize];
Ex:int [] a=new int[4];
C++To declare an array in C++, specify the type of the elementsand the number of elements required by an array as follows:
type arrayName [ arraySize ];
Ex:int a[4];
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 25 / 28
Java to C++ Transition Objects Creation
Java Vs C++: Objects Creation
1 class Abc
2 { ...
3 }
Java:You can create an object by using the new operator with thefollowing syntax:
Class name obj reference= new Class();Ex: Abc a= new Abc();C++To declare an array in C++, specify the type of the elementsand the number of elements required by an array as follows:Classname obj reference;Ex: Abc a;
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 26 / 28
Java to C++ Transition Objects Creation
C++ Some Additional Features
Pointers
Destructors
Operator Overloading
Multiple Inheritance
Struct,Enum and Union
Supports Goto Statement
Akhil Krishnan (Department of Computing and Software McMaster University)C++ Fundamentals, Conditionals, Loops, Functions 27 / 28