Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
EditEdit
MasterMaster
texttext stylesstyles
Practical Lecture 3: Concepts &
Basics of C++ Programming
Let’s take a quick recap of previous lecture –
A)
B)
C)
D)
E)
Quick Recap
Today we are going to cover -
● C++ Introduction.
● Difference b/w C and C++.
● Installation
● Syntax
● Variable
● Data Types
Today’s Agenda
● Array
● String
● Function
● Programming Practice Questions
● MCQ Questions
● Revision
Today’s Agenda
Let’s Get Started-
C++
C++ Introduction
C++ is a general-purpose programming language created by Bjarne Stroustrup as an
extension of the C programming language, or "C with Classes".
The language has expanded significantly over time, and modern C++ now has object-oriented,
generic, and functional features in addition to facilities
for low-level memory manipulation. It is almost always implemented as a compiled language
C++ Introduction
C++ supports the object-oriented programming, the four major pillar of object-
oriented programming (OOPs) used in C++ are:
Inheritance :-In C++, inheritance is a process in which one object acquires all the properties
and behaviors of its parent object automatically. In such way, you can reuse, extend or modify
the attributes and behaviors which are defined in other class. ... The derived class is the
specialized class for the base class
C++ Introduction
Polymorphism:-Polymorphism means "many forms", and it occurs when we have many
classes that are related to each other by inheritance.
C++ Introduction
Encapsulation:-In object-oriented programming (OOP), encapsulation refers to the bundling
of data with the methods that operate on that data, or the restricting of direct access to some
of an object's components. ... Publicly accessible methods are generally provided in the class
to access or modify the state more abstractly.
C++ Introduction
Abstraction:-Data Abstraction is a process of providing only the essential details to the
outside world and hiding the internal details, i.e., representing only the essential details in the
program. Data Abstraction is a programming technique that depends on the seperation of the
interface and implementation details of the program.
C++ Introduction
By the help of C++ programming language, we can develop different types of secured and
robust applications:
• Window application
• Client-Server application
• Device drivers
• Embedded firmware
Usage Of C++
Difference b/w c and C++.
On Visual Studio code:-https://code.visualstudio.com/docs/languages/cpp
One Code Blocks:-
https://www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html
Installation
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello World!";
return 0;
}
Hello World in C++
A variable is a name which is associated with a value that can be changed. For example when I write int
num=20; here variable name is num which is associated with value 20, int is a data type that represents
that this variable can hold integer values.
Types of variable:-
int: These type of of variables holds integer value.
char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.
bool: holds boolean value true or false.
double: double-precision floating point value.
float: Single-precision floating point value.
Variables in C++
1. Global variable:-A variable declared outside of any function (including main as well) is
called global variable. Global variables have their scope throughout the program, they can be
accessed anywhere in the program, in the main, in the user defined function, anywhere.
2. Local variable:-Local variables are declared inside the braces of any user defined function,
main function, loops or any control statements(if, if-else etc) and have their scope limited
inside those
Variable Scope
A data type or simply type is an attribute of data which tells the compiler or interpreter how
the programmer intends to use the data. ... This data type defines the operations that can be
done on the data, the meaning of the data, and the way values of that type can be stored.
Data Types
• Write a program to swap two number without using third variable.
• Write a program to find the size of all the data types. int, long, float, double, char, string,
bool
Practice Question's
Sometimes we need to execute a block of statements only when a particular condition is met
or not met. This is called decision making, as we are executing a certain code after making a
decision in the program logic. For decision making in C++, we have four types of control
statements (or control structures), which are as follows:
a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement
Conditionals:-
• Write a program to check whether a number is prime or not.
• Write a program to print the greatest among the three numbers entered by the user.
• Write a program to check whether the year is leap or not.
• Write a program to check whether the digit is palindrome or not.
Practice Question's
An array is a collection of similar items stored in contiguous memory locations. In
programming, sometimes a simple variable is not enough to hold all the data. For example,
let's say we want to store the marks of 500 students, having 500 different variables for this
task is not feasible, we can define an array with size 500 that can hold the marks of all
students
Array
Method 1:-int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
Method 2:-
int arr[] = {10, 20, 30, 40, 50};
Method 3:-
int arr[5] = {10, 20, 30, 40, 50};
Declaring array
#include <iostream>
using namespace std;
int main(){
int arr[] = {11, 22, 33, 44, 55};
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
cout<<arr[4]<<endl;
return 0;
}
Accessing Array
1.Write a program to print the sum and product of all the number in the given array.
2.Write a program to find the greatest number in the array.
Practice Questions
Strings
A string is a data type used in programming, such as an integer and floating point unit, but is
used to represent text rather than numbers. ... For example, the word "hamburger" and the
phrase "I ate 3 hamburgers" are both strings. Even "12345" could be considered a string, if
specified correctly.
#include <iostream>
#include <string>
using namespace std;
int main() {
string enterText;
cin>>enterText;
cout << enterText;
return 0;
}
Practice Questions
1. Write a program to find the frequency of character in the string .
2. Write a program to reverse a string.
A function is block of code which is used to perform a particular task, for example let’s say
you are writing a large C++ program and in that program you want to do a particular task
several number of times, like displaying value from 1 to 10, in order to do that you have to
write few lines of code and you need to repeat these lines every time you display values.
Another way of doing this is that you write these lines inside a function and call that function
every time you want to display values. This would make you code simple, readable and
reusable.
Functions
#include <iostream>
using namespace std;
/* This function adds two integer values
* and returns the result
*/int
sum(int num1, int num2){
int num3 = num1+num2; return num3;
}
int main(){
//Calling the function
cout<<sum(1,99);
return 0;
}
Function
Function
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double num, squareRoot;
cout << "Enter number: ";
cin >> num;
squareRoot = sqrt(num);
cout << "The square root of " << num << " is: " << squareRoot;
return 0;
}
Build In Function
#include <iostream>
using namespace std;
void sayHello() {
cout << "Hello!";
}
int main() {
sayHello();
return 0;
}
User Defined Function
• Define a function that returns the product of two numbers entered by user.
• Write a program to print the circumference and area of a circle of radius entered by user
by defining your own function.
• A person is elligible to vote if his/her age is greater than or equal to 18. Define a function
to find out if he/she is elligible to vote.
Practice Questions
• Write a program which will ask the user to enter his/her marks (out of 100). Define
a function that will display grades according to the marks entered as below:
Marks Grade
91-100 AA
81-90 AB
71-80 BB
61-70 BC
51-60 CD
41-50 DD
<=40 Fail
Practice Questions
1. What is difference between c and c++?
2. What is oop's concept?
3. What is function explain user defined and pre defined function.
Revision
• C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String.
• C++ Program to Check Armstrong Number
In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number
itself. For example, 153 is an Armstrong number because
153 = 1*1*1 + 5*5*5 + 3*3*3
• C++ Program to Concatenate Two Strings
Assignment For You
Any Questions??
QNA Time
Any Questions ??
Thank You!
See you guys in next class.

More Related Content

c++ referesher 1.pdf

  • 1. EditEdit MasterMaster texttext stylesstyles Practical Lecture 3: Concepts & Basics of C++ Programming
  • 2. Let’s take a quick recap of previous lecture – A) B) C) D) E) Quick Recap
  • 3. Today we are going to cover - ● C++ Introduction. ● Difference b/w C and C++. ● Installation ● Syntax ● Variable ● Data Types Today’s Agenda
  • 4. ● Array ● String ● Function ● Programming Practice Questions ● MCQ Questions ● Revision Today’s Agenda
  • 6. C++ Introduction C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significantly over time, and modern C++ now has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation. It is almost always implemented as a compiled language
  • 7. C++ Introduction C++ supports the object-oriented programming, the four major pillar of object- oriented programming (OOPs) used in C++ are: Inheritance :-In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class. ... The derived class is the specialized class for the base class
  • 9. Polymorphism:-Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance. C++ Introduction
  • 10. Encapsulation:-In object-oriented programming (OOP), encapsulation refers to the bundling of data with the methods that operate on that data, or the restricting of direct access to some of an object's components. ... Publicly accessible methods are generally provided in the class to access or modify the state more abstractly. C++ Introduction
  • 11. Abstraction:-Data Abstraction is a process of providing only the essential details to the outside world and hiding the internal details, i.e., representing only the essential details in the program. Data Abstraction is a programming technique that depends on the seperation of the interface and implementation details of the program. C++ Introduction
  • 12. By the help of C++ programming language, we can develop different types of secured and robust applications: • Window application • Client-Server application • Device drivers • Embedded firmware Usage Of C++
  • 13. Difference b/w c and C++.
  • 14. On Visual Studio code:-https://code.visualstudio.com/docs/languages/cpp One Code Blocks:- https://www3.ntu.edu.sg/home/ehchua/programming/howto/CodeBlocks_HowTo.html Installation
  • 15. #include<iostream> using namespace std; int main() { cout<<"Hello World!"; return 0; } Hello World in C++
  • 16. A variable is a name which is associated with a value that can be changed. For example when I write int num=20; here variable name is num which is associated with value 20, int is a data type that represents that this variable can hold integer values. Types of variable:- int: These type of of variables holds integer value. char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc. bool: holds boolean value true or false. double: double-precision floating point value. float: Single-precision floating point value. Variables in C++
  • 17. 1. Global variable:-A variable declared outside of any function (including main as well) is called global variable. Global variables have their scope throughout the program, they can be accessed anywhere in the program, in the main, in the user defined function, anywhere. 2. Local variable:-Local variables are declared inside the braces of any user defined function, main function, loops or any control statements(if, if-else etc) and have their scope limited inside those Variable Scope
  • 18. A data type or simply type is an attribute of data which tells the compiler or interpreter how the programmer intends to use the data. ... This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored. Data Types
  • 19. • Write a program to swap two number without using third variable. • Write a program to find the size of all the data types. int, long, float, double, char, string, bool Practice Question's
  • 20. Sometimes we need to execute a block of statements only when a particular condition is met or not met. This is called decision making, as we are executing a certain code after making a decision in the program logic. For decision making in C++, we have four types of control statements (or control structures), which are as follows: a) if statement b) nested if statement c) if-else statement d) if-else-if statement Conditionals:-
  • 21. • Write a program to check whether a number is prime or not. • Write a program to print the greatest among the three numbers entered by the user. • Write a program to check whether the year is leap or not. • Write a program to check whether the digit is palindrome or not. Practice Question's
  • 22. An array is a collection of similar items stored in contiguous memory locations. In programming, sometimes a simple variable is not enough to hold all the data. For example, let's say we want to store the marks of 500 students, having 500 different variables for this task is not feasible, we can define an array with size 500 that can hold the marks of all students Array
  • 23. Method 1:-int arr[5]; arr[0] = 10; arr[1] = 20; arr[2] = 30; arr[3] = 40; arr[4] = 50; Method 2:- int arr[] = {10, 20, 30, 40, 50}; Method 3:- int arr[5] = {10, 20, 30, 40, 50}; Declaring array
  • 24. #include <iostream> using namespace std; int main(){ int arr[] = {11, 22, 33, 44, 55}; cout<<arr[0]<<endl; cout<<arr[1]<<endl; cout<<arr[2]<<endl; cout<<arr[3]<<endl; cout<<arr[4]<<endl; return 0; } Accessing Array
  • 25. 1.Write a program to print the sum and product of all the number in the given array. 2.Write a program to find the greatest number in the array. Practice Questions
  • 26. Strings A string is a data type used in programming, such as an integer and floating point unit, but is used to represent text rather than numbers. ... For example, the word "hamburger" and the phrase "I ate 3 hamburgers" are both strings. Even "12345" could be considered a string, if specified correctly. #include <iostream> #include <string> using namespace std; int main() { string enterText; cin>>enterText; cout << enterText; return 0; }
  • 27. Practice Questions 1. Write a program to find the frequency of character in the string . 2. Write a program to reverse a string.
  • 28. A function is block of code which is used to perform a particular task, for example let’s say you are writing a large C++ program and in that program you want to do a particular task several number of times, like displaying value from 1 to 10, in order to do that you have to write few lines of code and you need to repeat these lines every time you display values. Another way of doing this is that you write these lines inside a function and call that function every time you want to display values. This would make you code simple, readable and reusable. Functions
  • 29. #include <iostream> using namespace std; /* This function adds two integer values * and returns the result */int sum(int num1, int num2){ int num3 = num1+num2; return num3; } int main(){ //Calling the function cout<<sum(1,99); return 0; } Function
  • 31. #include <iostream> #include <cmath> using namespace std; int main() { double num, squareRoot; cout << "Enter number: "; cin >> num; squareRoot = sqrt(num); cout << "The square root of " << num << " is: " << squareRoot; return 0; } Build In Function
  • 32. #include <iostream> using namespace std; void sayHello() { cout << "Hello!"; } int main() { sayHello(); return 0; } User Defined Function
  • 33. • Define a function that returns the product of two numbers entered by user. • Write a program to print the circumference and area of a circle of radius entered by user by defining your own function. • A person is elligible to vote if his/her age is greater than or equal to 18. Define a function to find out if he/she is elligible to vote. Practice Questions
  • 34. • Write a program which will ask the user to enter his/her marks (out of 100). Define a function that will display grades according to the marks entered as below: Marks Grade 91-100 AA 81-90 AB 71-80 BB 61-70 BC 51-60 CD 41-50 DD <=40 Fail Practice Questions
  • 35. 1. What is difference between c and c++? 2. What is oop's concept? 3. What is function explain user defined and pre defined function. Revision
  • 36. • C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String. • C++ Program to Check Armstrong Number In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because 153 = 1*1*1 + 5*5*5 + 3*3*3 • C++ Program to Concatenate Two Strings Assignment For You
  • 38. Thank You! See you guys in next class.