0% found this document useful (0 votes)
44 views30 pages

Unit-4.1 PPT Notes

Uploaded by

shivam sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views30 pages

Unit-4.1 PPT Notes

Uploaded by

shivam sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Object Oriented Techniques

(Unit-4)

1
What is C?

C is a general purpose, procedural, imperative language developed in 1972 by


Dennis Ritchie at Bell Labs for the Unix Operating System.
 Low-level access to memory
 Language constructs close to machine instructions
 Used as a “machine-independent assembler”

2
My first C
Program
Include standard io
declarations
A preprocessor directive
#include <stdio.h>
Write to
standar int main(void)
d {
output printf("hello, world\n");
return 0;
}
char array
Indicate correct termination
3
What is C++ ?
 C++ is the C programmer‟s answer to Object-Oriented Programming (OOP).
 C++ is an enhanced version of the C language.
C++ adds support for OOP without sacrificing any of C‟s power, elegance, or
flexibility.
 C++ was invented in 1979 by Bjarne Stroustrup at Bell Laboratories in
Murray
Hill,
The New
elements of USA.
Jersey, a computer language do not exist in a void, separate from one
another.
 The features of C++ are highly integrated.
Both object-oriented and non-object-oriented programs can be developed
using C++.

4
WHAT IS OOP?

 OOP is a powerful way to approach the task of programming.


 OOP encourages developers to decompose a problem into its constituent parts.
Each component becomes a self-contained object that contains its own
instructions and data that relate to that object.
 So, complexity is reduced and the programmer can manage larger programs.

5
WHAT IS OOP?
(CONT.)
 All OOP languages, including C++, share three common defining
traits:
 Encapsulation
 Binds together code and data
 Polymorphism
 Allows one interface, multiple methods
 Inheritance
 Provides hierarchical classification
 Permits reuse of common code and data

6
TWO VERSIONS OF C+
+
 A traditional - style C++ progr a m
-
# include < iostream. h>

int main()
{

/* program code */ return 0;

7
TWO VERSIONS OF C++ (CONT.)

 A modern- style C++ program that uses the new- style headers
and a
namespace -
# include < iostream>
using namespace
std;

int main()
{

/* program code */

return 0;
8
THE NEW C++ HEADERS

 The new-style headers do not specify filenames.


#include <Library_Name>
 They simply specify standard identifiers that might be mapped to files by the
compiler, but they need not be.
<iostream>
<vector>
<string>, not related with <string.h>
<cmath>, C++ version of <math.h>
<cstring>, C++ version of <string.h>
.

9
N AMES
PAC
namespace is used to define a scope ES
that could hold global identifiers.
ex:-namespace scope for c++ standard library.
 A namespace is a declarative region.
 It localizes the names of identifiers to avoid name collisi
 +-------------------------------------------------------------------------------ons.
 The contents of new-style headers are placed in the std namespace.
 namespace "std“ Has all standard library definitions we need
A newly created class, function or global variable can put in an existing namespace, a
new namespace, or it may not be associated with any namespace
 In the last case the element will be placed in the global unnamed namespace.

10
Namespaces
Examples:

 #include <iostream>
using namespace
std;
Includes entire
standard library of
name definitions

 #include <iostream>using
std::cin; using std::cout;
Can specify just the objects we
want
11
Special Symbols

+ ?
- ,
* <=
/
!=
.
; ==
>=

12
Keywords
• Reserved words, keywords, or word symbols
• Include:
• int
• float
• double
• char
• const
• void
• return

(Note: There are a total of 95 reserved words in C++. The reserved words of C++ may be conveniently placed into
several groups. In the first group, we put those that were also present in the C programming language and have been
carried over into C++. There are 32 of these.)

13
Identifiers
Identifiers refer to the names of variables, functions, arrays, classes, etc. created by
programmer.
The following rules are common to both C and C++.
 Only alphabet characters, digits and underscores are permitted.
 The name cannot start with a digit.
 Uppercase and lowercase letters are distinct.
 A declared keyword cannot be used as a variable name

14
Variable
s that can hold values. Before assigning any value
Variables are locations in the memory
to a variable, it must be declared. To use the variable number storing an integer value,
the variable number must be declared and it should be of the type int as follows:
int number;
 Variables
int feet; //variable to hold given feet
int inches; //variable to hold given inches
int totalInches; //variable to hold total inches
double centimeters; //variable to hold length in
//centimeters

15
Constants
 Constants refer to fixed values that do not change during the execution of a program.
Like C, C++
Support several kinds of literal constants. They include integers, characters, floating
point numbers and string.
 Named Constant
const double CENTIMETERS_PER_INCH = 2.54;
const int INCHES_PER_FOOT = 12;

16
C++ K EY W ORD S ( PARTIAL
LIST) □ bool □ protected
□ catch □ public
□ delete □ template
□ false □ this
□ friend □ throw
□ inline □t rue
□ namespace □t r y
□ new □ using
□ operator □ virtual
□ private □ wchar_t

17
DATA TYPES IN C+
+

18
Enumerated Data Type
An enumerated data type is another user-defined type which provides a way for
attaching names to numbers, thereby increasing comprehensibility of the code. The
enum keyword automatically enumerates a list of words by assigning them values
0,1,2 and so on. The syntax of an enum statement is similar to that of the struct
statement. Ex: enum shape { circle, square, triangle };
enum color { red, green, blue,
By default, the enumeratorsyellow };
are assigned integer values starting with 0 for the first
enumerator, 1 for the second, and so on. But we can give a name, a specific value by
adding an initializer. For example, enum color{red, blue=4, green=8};
enum color{red=5, blue, green};
are valid definitions. In first case, red is 0 by default. In the second case, blue is 6
and green is 7.

19
Operator

 All operators in C are valid in C++ also.


An operator is a symbol which represents a particular operation that can
be performed on data. An operand is the object on which an operation is performed.
By combining the operators and operands we form an expression. An expression is a
sequence of operands and operators that reduces to a single value.

20
Operators in C++:
All above operators of c language are also valid in C++. New operators introduced
in C++ are Sno Operator Symbol

1 Scope resolution operator ::

2 Pointer to a member ::*


declarator
3 Pointer to member operator ->*,->

4 Pointer to member operator .*

5 new Memory allocating operator

6 delete Memory release operator

7 endl Line feed operator

8 setw Field width operator

9 insertion <<

10 Extraction >>

21
Scope Resolution operator
Scope:-Visibility or availability of a variable in a program is called as scope. There
are two types of scope.
i) Local scope ii)Global scope
 Local scope: visibility of a variable is local to the function in which it is declared.
 Global scope: visibility of a variable to all functions of a program Scope resolution
operator in “::” .
 This is used to access global variables if same variables are declared as local
and
global

22
#include<iostream.h>
int a=5;
void main()
{
int a=10;
cout<<”Local a=”<<a<<endl;
cout<<”Global
a=”<<::a<<endl;
}
Expected
output:
Local a=10
Global
a=5
23
Type Conversion (Casting)
 C++ permits explicit type conversion of variables or expression using the
type cast operator.
(type-name) expression //C notation
type-name (expression) //C++ notation
Examples:
average = sum/(float) // C notation
average=sum/float(i); // C++ notation

24
Control Structures
 A computer can proceed:
• In sequence
• Selectively (branch) - making a choice
• Repetitively (iteratively) – looping

Some statements are executed only if certain conditions are


met
• A condition is met if it evaluates to true

25
Control Structures (continued)

26
C++ C O N S O L E I/O
( O U T P U T ) << “Hello World!”;
cout
printf(“Hello World!”);
 cout << iCount; /* int iCount */
printf(“%d”, iCount);
 cout << 100.99;
 printf(“%f”, 100.99);
 cout << „\n‟, or cout << endl
 printf(“\n”)
 general, cout << expression;
In

27
C++ CONSOLE I/O
 cin >>(str
INPUT)
Name ; /* ch ar
strName[16] */ scanf(“%s”, strName);
 cin >> iCount; /* int
iCount */ scanf(“%d”, &iCount);
 cin >> fValue; /* float
fValue */
□ scanf(“%f”, &fValue);

I n general, cin >> variable;

28
C++ C O N S O L E I/O
( EXAMPLE)
include <iostream> include <iostream>
int main() using namespace std;
{ int main()
char str[16]; {
std::cout << “Enter a string: ”; char str[16];
std::cin >> str; cout << “Enter a string: ”;
std::cout << “You entered: ” cin >> str;
<< str; cout << “You entered: ”<< str;
} }

29
Thank
You
30

You might also like