Introduction to c ++ part -1
-
Upload
baabtracom-no-1-supplier-of-quality-freshers -
Category
Technology
-
view
282 -
download
1
Transcript of Introduction to c ++ part -1
![Page 1: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/1.jpg)
Recall
• What are files handling used for?
• How do we write in to a file?
• What are the different modes of opening a
file?
• What is fgetc() ?
![Page 2: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/2.jpg)
Introduction to C++Week 4- day 1
![Page 3: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/3.jpg)
C++
• Super Set of CC++ was originally developed to be the next version of C, not a new language.
• Backward compactable with C
Most of the C functions can run in C++
• Same compiler preprocessorMust have a function names “main” to determine where the program starts
![Page 4: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/4.jpg)
Where they born
Bell Labs
![Page 5: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/5.jpg)
C Vs C++Similarities
![Page 6: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/6.jpg)
C Vs C++
• int
• Char
• Float
• double
“Exactly the same as
of left side”
Data Types
![Page 7: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/7.jpg)
C Vs C++
• Conditional control
structures– If
– If else
– Switch
• Loops– for
– while
– Do while
“Exactly the same as
of left side”
Control Structures
![Page 8: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/8.jpg)
C Vs C++
int sum(int a, int b)
{
Int c;
c=a + b
return c;
}
sum(12,13)
“Exactly the same as
of left side”
functions
![Page 9: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/9.jpg)
C Vs C++
Int a[10];
Int b[] = {1000, 2, 3,
50}; “Exactly the same as
of left side”
Arrays
![Page 10: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/10.jpg)
C Vs C++Differences
![Page 11: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/11.jpg)
C Vs C++
Output
printf(“ value of a = %d”,a);
Printf(“a = %d and b= %d”,a,b);
Input
scanf(“%d ", &a);
Scanf(“%d”,&a,&b);
Output
cout<< “value of a =" << a;
Cout<<“a =”<<a<<“b=”<<b;
Input
cin>>a;
Cin>>a>>b;;
Input / Output
![Page 12: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/12.jpg)
C Vs C++
char str[10];
str = "Hello!"; //ERROR
char str1[11] = "Call
home!";
char str2[] = "Send
money!";
char str3[] = {'O', 'K',
'\0'};
strcat(str1, str2);
strcpy(str, "Hello!");
string str;
str = "Hello";
string str1("Call
home!");
string str2 = "Send
money!";
string str3("OK");
str = str1 + str2;
str = otherString;
Strings
![Page 13: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/13.jpg)
C Vs C++
struct Data
{
int x;
};
struct Data module;
module.x = 5;
struct Data
{
int x;
void printMe()
{
cout<<x;
}
} Data;
Data module,Module2;
module.x = 5;
module2.x = 12;
module.printMe() // Prints 5
module.printMe() // Prints 12
Structures
![Page 14: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/14.jpg)
What’s New in C++ ?
![Page 15: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/15.jpg)
• OOP concepts
• Operator overloading
What’s New in C++ ?
![Page 16: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/16.jpg)
OOP Concept
• Object-oriented programming (OOP) is a
style of programming that focuses on
using objects to design and build
applications.
• Think of an object as a model of the
concepts, processes, or things in the real
![Page 17: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/17.jpg)
Objects in real World
![Page 18: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/18.jpg)
Real World Objects
![Page 19: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/19.jpg)
Objects in real world
• Object will have an identity/name
Eg: reynolds, Cello for pen. Nokia,apple for
mobile
• Object will have different properties which
describes them best
Eg:Color,size,width
• Object can perform different actions
Eg: writing,erasing etc for pen. Calling,
texting for mobile
![Page 20: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/20.jpg)
Objects
I have an identity: I'm Volkswagen
I have different properties.My color property is green
My no:of wheel property is 4
I can perform different actionsI can be drived
I can consume fuelI can play Music
I have an identity: I'm Suzuki
I have different properties.My color property is silver
My no:of wheel property is 4
I can perform different actionsI can be drived
I can consume fuelI can play Music
![Page 21: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/21.jpg)
How these objects are created?
• All the objects in the real world are created
out of a basic prototype or a basic blue
print or a base design
![Page 22: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/22.jpg)
Objects in software World
![Page 23: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/23.jpg)
Objects in the software world
• Same like in the real world we can create
objects in computer programming world
– Which will have a name as identity
– Properties to define its behaviour
– Actions what it can perform
![Page 24: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/24.jpg)
How these objects are
created• We need to create a base design which
defines the properties and functionalities
that the object should have.
• In programming terms we call this base
design as Class
• Simply by having a class we can create
any number of objects of that type
![Page 25: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/25.jpg)
Definition
• Class : is the base design of
objects
• Object : is the instance of a class
• No memory is allocated when a class is
created.
• Memory is allocated only when an object is
created.
![Page 26: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/26.jpg)
How to create class in
C++class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
public:
![Page 27: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/27.jpg)
How to create class in
C++class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Is the Keyword to create any class
public:
![Page 28: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/28.jpg)
How to create class in
C++class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Is the name of the class
public:
![Page 29: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/29.jpg)
How to create class in
C++class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Called access specifier. Will detailed soonpublic:
![Page 30: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/30.jpg)
How to create class in
C++class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Are two variable that referred as the properties
public:
![Page 31: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/31.jpg)
How to create class in
C++class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Is the only functionality of this class
public:
![Page 32: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/32.jpg)
How to create objects in
C++shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Is the class name
![Page 33: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/33.jpg)
How to create objects in
C++shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Two objects of base type shape
![Page 34: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/34.jpg)
How to create objects in
C++shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Setting properties of object named rectangle
![Page 35: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/35.jpg)
How to create objects in
C++shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Setting properties of object named square
![Page 36: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/36.jpg)
How to create objects in
C++shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Calling the functionality of rectangle which is claclulateArea();
![Page 37: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/37.jpg)
How to create objects in
C++shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();Calling the functionality of rectangle which is claclulateArea();
![Page 38: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/38.jpg)
Example
Class : shape
Height:35 width:20
Object rectangle
calculateArea(){Return 20*35}
Height:10 width:10
Object square
calculateArea(){Return 10*10;}
Member variablesHeightwidth
Member functioncalculateArea{return height*width;
}
![Page 39: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/39.jpg)
Access Specifier
• Access specifiers defines the access
rights for the statements or functions that
follows it until another access specifier or
till the end of a class.
• The three types of access specifiers are
– Private
– Public
– Protected
![Page 40: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/40.jpg)
Access Specifier
class Base
{
public:
// public members go here
protected:
// protected members go here
private:
// private members go here
};
![Page 41: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/41.jpg)
Class Vs Structure
• Class is similar to Structure.
• Structure members have public access by
default.
• Class members have private access by
default.
![Page 42: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/42.jpg)
Self Check !!
![Page 43: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/43.jpg)
Self Check
• Which of the following term is used for a
function defined inside a class?
– Member Variable
– Member function
– Class function
– Classic function
![Page 44: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/44.jpg)
Self Check
• Which of the following term is used for a
function defined inside a class?
– Member Variable
– Member function
– Class function
– Classic function
![Page 45: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/45.jpg)
Self Check
• cout is a/an __________ .
– Operator
– Function
– Object
– macro
![Page 46: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/46.jpg)
Self Check
• cout is a/an __________ .
– Operator
– Function
– Object
– macro
![Page 47: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/47.jpg)
Self Check
• Which of the following is correct about
class and structure?
– class can have member functions while
structure cannot.
– class data members are public by default
while that of structure are private.
– Pointer to structure or classes cannot be
declared.
– class data members are private by default
while that of structure are public by default.
![Page 48: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/48.jpg)
Self Check
• Which of the following is correct about
class and structure?
– class can have member functions while
structure cannot.
– class data members are public by default
while that of structure are private.
– Pointer to structure or classes cannot be
declared.
– class data members are private by default
while that of structure are public by default.
![Page 49: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/49.jpg)
Self Check
• Which of the following operator is
overloaded for object cout?
• >>
• <<
• +
• =
![Page 50: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/50.jpg)
Self Check
• Which of the following operator is
overloaded for object cout?
• >>
• <<
• +
• =
![Page 51: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/51.jpg)
Self Check
• Which of the following access
specifier is used as a default in a
class definition?
• protected
• public
• private
• friend
![Page 52: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/52.jpg)
Self Check
• Which of the following access
specifier is used as a default in a
class definition?
• protected
• public
• private
• friend
![Page 53: Introduction to c ++ part -1](https://reader036.fdocuments.in/reader036/viewer/2022081404/559b74c41a28ab6f4f8b4695/html5/thumbnails/53.jpg)
End of Day