0% found this document useful (0 votes)
19 views

Unit-4.1 PPT Notes

This document provides an overview of object oriented techniques in C++. It discusses key concepts like classes, objects, encapsulation, inheritance and polymorphism. It also covers C++ basics like variables, data types, operators, comments and namespaces. The document compares traditional and modern C++ styles and lists some common keywords in C++.

Uploaded by

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

Unit-4.1 PPT Notes

This document provides an overview of object oriented techniques in C++. It discusses key concepts like classes, objects, encapsulation, inheritance and polymorphism. It also covers C++ basics like variables, data types, operators, comments and namespaces. The document compares traditional and modern C++ styles and lists some common keywords in C++.

Uploaded by

Bruce Wayne
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Object Oriented Techniques

(Unit-4)

by
Surendra Kr.Keshari
Assistant Professor
KIET Group of Institutions

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
standard int main(void)
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, New Jersey, USA.
The elements of 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++ progra 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 ES
namespace is used to define a scope 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 collisions.
 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
C++ C O M M E N T S

Two types:
 Single line
// This is a C++ program. It prints the sentence:
// Welcome to C++ Programming.

 Multiple line
/*
You can include comments that can occupy several lines.
*/

12
Special Symbols

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

13
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.)

14
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

15
Variables
 Variables are locations in the memory that can hold values. Before assigning any value
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

16
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;

17
C++ K E Y W O R D S (PARTIAL LIS T)
bool protected
catch public
delete t e m p l a t e
false t h i s
friend t hrow
inline t r u e
na me spa c e t r y
new using
operator vi rt ua l
pri va t e wchar_t

18
DATA TYPES IN C++

19
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, yellow };
 By default, the enumerators 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.

20
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.

21
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 >>

22
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

23
#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

24
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

25
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

26
Control Structures (continued)

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

28
C++ CONSOLE I/O (INPUT)
 cin >> strName ; /* char strName[16] */
scanf(“%s”, strName);
 cin >> iCount; /* in t iCount */
scanf(“%d”, &iCount);
 cin >> fValue; /* float fValue */
 scanf(“%f”, &fValue);

I n general, cin >> variable;

29
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: ” << str; cin >> str;
} cout << “You entered: ”<< str;
}

30
Thank
You
31

You might also like