PROGRAMMING WITH C & C++

UNIT-3

1. Difference Between C and C++

Parameter

C

C++

Programming Paradigm

C is a structural or procedural programming language. 

C is a structural as well as an object-oriented programming language.

History

C was developed by scientist Dennis Ritchie in 1972 at Bell Laboratories.

C was developed by Bjarne Stroustup in 1979.

Approach

C follows a top-down approach 

C follows the bottom-up approach.

Keywords

C contains 32 keywords

C++ contains 63 keywords.

Data Types

C supports built-in data types.

C++ support both built-in and user-defined data types.

File Extension

.c is the file extension for C programming language

.cpp is the file extension for C++ programming language

Header File

<stdio.h> header file is used by C language

<iostream.h> header file is used by C++ language

Access Modifier

C language does not support access modifier

C++ support access modifier

Security

C does not have any security features so it can be manipulated by outsider

C++ is a secure language as it offers security features such as data hiding and encapsulation

Reference Variable

C does not support reference variable

C++ support reference variable

Function Overloading and Function Overriding

C don’t supports function overloading and function overriding

C++ supports function overloading and function overriding

Exception Handling

C does not support exception handling directly, it uses the function that support exception handling

C++ directly support exception handling with the help of try – catch block

Program Division

C is a procedural language, so code written in C are divided in separate blocks known as function

C++ is a object oriented language, so code and divided into classes and objects

Inline Function

C doesn’t support inline function

C++ support inline function

Driven Type

C is known as function driven language

C is known as object driven language

Data and Function

In C, the data and function are separated as it is a procedural programming language

In C++, the data and function are encapsulated as it is a object oriented programming language

Input and Output Function

In C scanf() and printf() functions are used to take the input and output respectively

In C++ cin and cout functions are used to take the input and output respectively

Application Development

C language is more suitable for low level implementation such as network driver, text editor, assembler, etc

C++ language is more suitable for high level implementation such as game development, smartwatches, embedded systems, etc

Namespace

To prevent the collision and organize the code, namespace is needed but C does not support that

C++ support the namespace

Used By

MySQL, Windows Kerne, Oracle Database, Telegram messenger, etc

Google Chrome, Torque 3-D game, Microsoft Office, etc

 

2.Function Overloading

With function overloading, multiple functions can have the same name with different parameters:

Syntax:
int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)

Example:

#include <iostream>

int addition(int x, int y) 
{
  return x + y;
}

int addition(int x, int y,int z)
{
  return x + y+z;
}


int main()
{
  int myNum1 = addition(8, 5);
  int myNum2 = addition(4.3, 6.26);
  cout << "Int: " << myNum1 << "\n";
  cout << "Double: " << myNum2;
  return 0;
}

3. Explain Classes , Objects and Data Members

C++ What are Classes and Objects?

Classes and objects are the two main aspects of object-oriented programming.

Look at the following illustration to see the difference between class and objects:

class

Fruit

objects

Apple

Banana

Mango

Another example:

class

Car

objects

Volvo

Audi

Toyota

 

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)
};

Create an Object

MyClass obj;

class MyClass {       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)
   };


int main() {
  MyClass myObj;  // Create an object of MyClass

  // Access attributes and set values
  myObj.myNum
 = 15

  

  // Print attribute values
  cout << myObj.myNum << "\n";
  cout << myObj.myString;
  return 0;
}

 

Data Members and Member Functions in C++ programming

Data members and Member functions in C++

"Data Member" and "Member Functions" are the new names/terms for the members of a class, which are introduced in C++ programming language.

The variables which are declared in any class by using any fundamental data types (like int, char, float etc) or derived data type (like class, structure, pointer etc.) are known as Data Members. And the functions which are declared either in private section of public section are known as Member functions.

There are two types of data members/member functions in C++:

  1. Private members
  2. Public members

1) Private members

The members which are declared in private section of the class (using private access modifier) are known as private members. Private members can also be accessible within the same class in which they are declared.

2) Public members

The members which are declared in public section of the class (using public access modifier) are known as public members. Public members can access within the class and outside of the class by using the object name of the class in which they are declared.

 

4. OOPS concepts

As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

OOPs Concepts:

· Class

· Objects

· Data Abstraction

· Encapsulation

· Inheritance

· Polymorphism

 

 

1. Class:

A class is a user-defined data type. It consists of data members and member functions, which can be accessed and used by creating an instance of that class. It represents the set of properties or methods that are common to all objects of one type. A class is like a blueprint for an object.

For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class, and wheels, speed limits, mileage are their properties.

2. Object:

It is a basic unit of Object-Oriented Programming and represents the real-life entities. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. For example “Dog” is a real-life Object, which has some characteristics like color, Breed, Bark, Sleep, and Eats.

 

 

Data Abstraction:

Data abstraction is one of the most essential and important features of object-oriented programming. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation.

 

4. Encapsulation:

Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. In Encapsulation, the variables or data of a class are hidden from any other class and can be accessed only through any member function of their class in which they are declared.

 

5. Inheritance:

Inheritance is an important pillar of OOP(Object-Oriented Programming). The capability of a class to derive properties and characteristics from another class is called Inheritance. When we write a class, we inherit properties from other classes. So when we create a class, we do not need to write all the properties and functions again and again, as these can be inherited from another class that possesses it. Inheritance allows the user to reuse the code whenever possible and reduce its redundancy.

 

6. Polymorphism:

The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. For example, A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.

 






Comments

Popular posts from this blog