Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Basics of C

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Basics of C++OOP

About c++oop:

Object oriented programming is a type of programming which


uses objects and classes its functioning. The object oriented
programming is based on real world entities like inheritance,
polymorphism, data hiding, etc. It aims at binding together
data and function work on these data sets into a single entity
to restrict their usage.
Some basic concepts of object oriented programming are −
1.CLASS
2.OBJECTS
3.ENCAPSULATION
4.POLYMORPHISM
5.INHERITANCE
6.ABSTRACTION
Object-oriented programming has several advantages over
procedural programming:

OOP is faster and easier to execute


OOP provides a clear structure for the programs
OOP helps to keep the C++ code DRY "Don't Repeat Yourself",
and makes the code easier to maintain, modify and debug
OOP makes it possible to create full reusable applications with
less code and shorter development time
C++ Classes/Objects:
C++ Classes/Objects:
C++ is an object-oriented programming language.
Everything in C++ is associated with classes and
objects, along with its attributes and methods. For
example: in real life, a car is an object. The car
has attributes, such as weight and color, and methods,
such as drive and brake.
Attributes and methods are
basically variables and functions that belongs to the
class. These are often referred to as "class members".
A class is a user-defined data type that we can use in
our program, and it works as an object constructor, or
a "blueprint" for creating objects.
Create a Class

To create a class, use the class keyword:


Example

Create a class called "MyClass":


class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string
variable)
};
Example explained
•The class keyword is used to create a class
called MyClass.

•The public keyword is an access specifier, which


specifies that members (attributes and methods) of
the class are accessible from outside the class. You
will learn more about access specifiers later.

•Inside the class, there is an integer


variable myNum and a string variable myString. When
variables are declared within a class, they are
called attributes.

•At last, end the class definition with a semicolon ;.


Access Specifiers

By now, you are quite familiar with the public keyword that
appears in all of our class examples:
Example
class MyClass { // The class
public: // Access specifier
// class members goes here
};
However, what if we want members to be
private and hidden from the outside world?
In C++, there are three access specifiers:

•public - members are accessible from outside


the class

•private - members cannot be accessed (or


viewed) from outside the class

•protected - members cannot be accessed from


outside the class, however, they can be
accessed in inherited classes. You will learn
more about Inheritance later.
demonstration of differences
between public and private members:

Example:

class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};

int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
Encapsulation
In object oriented programming, encapsulation is the
concept of wrapping together of data and information
in a single unit. A formale defination of encapsulation
would be: encapsulation is binding togather the data
and related function that can manipulate the data.
Polymorphism
The name defines polymorphism is multiple forms.
which means polymorphism is the ability of object
oriented programming to do some work using multiple
forms. The behaviour of the method is dependent on
the type or the situation in which the method is called.
Let’s take a real life example, A person can have more
than one behaviour depending upon the situation. like
a woman a mother, manager and a daughterAnd this
define her behaviour. This is from where the concept
of polymorphism came from.
In c++ programming language, polymorphism is
achieved using two ways. They are operator overloading
and function overloading.

Operator overloading In operator overloading and


operator can have multiple behaviour in different
instances of usage.

Function overloading Functions with the same name


that can do multiple types based on some condition
Inheritance
In C++, it is possible to inherit attributes and
methods from one class to another. We group the
"inheritance concept" into two categories:

•derived class (child) - the class that inherits from


another class

•base class (parent) - the class being inherited from


To inherit from a class, use the : symbol.
In the example below, the Car class (child) inherits
the attributes and methods from the Vehicle class
(parent):
Example
// Base class
class Vehicle {
public:
string brand = “BMW";
void honk() {
cout << “Kiit, Kiit! \n" ;
}
};

// Derived class
class Car: public Vehicle {
public:
string model = “Ford";
};

int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
Arrays in c++
Array

It is a group of variables of similar data types


referred to by a single element.
Its elements are stored in a contiguous memory
location.
The size of the array should be mentioned while
declaring it.
Array elements are always counted from zero (0)
onward.
Array elements can be accessed using the
position of the element in the array.
The array can have one or more dimensions.
An array in C/C++ or be it in any
programming language is a collection of
similar data items stored at contiguous
memory locations and elements can be
accessed randomly using indices of an
array. They can be used to store the
collection of primitive data types such as
int, float, double, char, etc of any particular
type. To add to it, an array in C/C++ can
store derived data types such as
structures, pointers etc. Given below is the
picture representation of an array.
#include <iostream>
using namespace std;

int main()
{
// Array declaration by specifying size and
initializing
// elements
int arr[6] = { 10, 20, 30, 40 };

// Compiler creates an array of size 6, initializes


first
// 4 elements as specified by user and rest two
elements as
// 0. above is same as "int arr[] = {10, 20, 30, 40,
0, 0}"

return 0;
}
Thanku

You might also like