C/C++

C Programming Language (C)??

Definition – What does C Programming Language (C) mean?
C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.

Ranked among the most widely used languages, C has a compiler for most computer systems and has influenced many popular languages ??? notably C++.

??

Techopedia explains??C Programming Language (C)

C belongs to the structured, procedural paradigms of languages. It is proven, flexible and powerful and may be used for a variety of different applications. Although high level, C and assembly language share many of the same attributes.

Some of C’s most important features include:

  • Fixed number of keywords, including a set of control primitives, such as if, for, while, switch and do while
  • Multiple logical and mathematical operators, including bit manipulators
  • Multiple assignments may be applied in a single statement.
  • Function return values are not always required and may be ignored if unneeded.
  • Typing is static. All data has type but may be implicitly converted.
  • Basic form of modularity, as files may be separately compiled and linked
  • Control of function and object visibility t
  • other files via extern and static attributes
  • ??for example
  • #include

    int main() {
    printf(“Hello, World!");
    return 0;
    }

    Levels of syntax

    Computer language syntax is generally distinguished into three levels:

    Words ??? the lexical level, determining how characters form tokens;
    Phrases ??? the grammar level, narrowly speaking, determining how tokens form phrases;
    Context ??? determining what objects or variables names refer to, if types are valid, etc.

    Distinguishing in this way yields modularity, allowing each level to be described and processed separately, and often independently. First a lexer turns the linear sequence of characters into a linear sequence of tokens; this is known as “lexical analysis" or “lexing". Second the parser turns the linear sequence of tokens into a hierarchical syntax tree; this is known as “parsing" narrowly speaking. Thirdly the contextual analysis resolves names and checks types. This modularity is sometimes possible, but in many real-world languages an earlier step depends on a later step ??? for example, the lexer hack in C is because tokenization depends on context. Even in these cases, syntactical analysis is often seen as approximating this ideal model.

    The parsing stage itself can be divided into two parts: the parse tree or “concrete syntax tree" which is determined by the grammar, but is generally far too detailed for practical use, and the abstract syntax tree (AST), which simplifies this into a usable form. The AST and contextual analysis steps can be considered a form of semantic analysis, as they are adding meaning and interpretation to the syntax, or alternatively as informal, manual implementations of syntactical rules that would be difficult or awkward to describe or implement formally.

    The levels generally correspond to levels in the Chomsky hierarchy. Words are in a regular language, specified in the lexical grammar, which is a Type-3 grammar, generally given as regular expressions. Phrases are in a context-free language (CFL), generally a deterministic context-free language (DCFL), specified in a phrase structure grammar, which is a Type-2 grammar, generally given as production rules in Backus???Naur form (BNF). Phrase grammars are often specified in much more constrained grammars than full context-free grammars, in order to make them easier to parse; while the LR parser can parse any DCFL in linear time, the simple LALR parser and even simpler LL parser are more efficient, but can only parse grammars whose production rules are constrained. In principle, contextual structure can be described by a context-sensitive grammar, and automatically analyzed by means such as attribute grammars, though in general this step is done manually, via name resolution rules and type checking, and implemented via a symbol table which stores names and types for each scope.

    Tools have been written that automatically generate a lexer from a lexical specification written in regular expressions and a parser from the phrase grammar written in BNF: this allows one to use declarative programming, rather than need to have procedural or functional programming. A notable example is the lex-yacc pair. These automatically produce a concrete syntax tree; the parser writer must then manually write code describing how this is converted to an abstract syntax tree. Contextual analysis is also generally implemented manually. Despite the existence of these automatic tools, parsing is often implemented manually, for various reasons ??? perhaps the phrase structure is not context-free, or an alternative implementation improves performance or error-reporting, or allows the grammar to be changed more easily. Parsers are often written in functional languages, such as Haskell, or in scripting languages, such as Python or Perl, or in C or C++.

C++:- OBJECT-ORIENTED PROGRAMMING (OOPS)

>

OOPs is basically a programming style that involves the concept of objects and class.We can also say that OOPs is an approach to a problem involving objects. Our objective is to tell a set of instructions to a computer with the help of programming language. To achieve this there are various ways of doing it and OOPs is one of those ways.

There are various concepts under OOPs which are;

1) Objects

2) Classes

3) Inheritance

4) Data encapsulation

5) Data abstraction

6) Polymorphism

We will discuss these concepts in details,

1) OBJECTS:

Objects are the basic unit of OOPs. Objects are basically instances of a class. We can say that class as the description of a concept, and an object as the realization of this description to create an independent distinguishable entity.

Objects can be concrete or conceptual each with its own individual identity.

These real-world objects share two characteristics: they all have state and they all have behavior. For example, we humans have hands, legs, eyes, nose, ears these are the data and walking, reading, writing, sleeping, eating are the methods.

Objects are made after real-world objects. A software object maintains its data in variables and implements the methods.

2) CLASSES:

Classes are basically a blueprint of the functional entity which defines its properties and function. Classes contain both data and functions. A class can have the subclass which can inherit data/properties of that class.

The above diagrams show an exact meaning of classes.

3) INHERITANCE:

Inheritance is a process in which properties of parent class are inherited by the child class. For example, if there is a parent class and a child class and we declare a variable or method in the parent class we can use that in child class without declaring that variable or method again.
) SINGLE INHERITANCE

Above diagram shows the concept of single inheritance.

In single inheritance base class derive the properties from only one parent class.

Syntax:

class subclass_name : access_mode base_class
{
//body of the subclass
};

EXAMPLE:

// C++ program to explain
// Single inheritance
#include
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// sub class derived from two base classes
class Car: public Vehicle{
};
// main function
int main()
{
// creating object of subclass will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output:
This is a vehicle

2) MULTIPLE INHERITANCE

In multiple inheritance properties of sub/child class are derived from two super/parent classes.

EXAMPLE:

// C++ program to explain
// multiple inheritance
#include
using namespace std;
// first base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// second base class
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" << endl;
}
};
// sub class derived from two base classes
class Car: public Vehicle, public FourWheeler {
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output:
This is a Vehicle
This is a 4 wheeler Vehicle

3) MULTILEVEL INHERITANCE:

In multilevel inheritance, a derived class is derived from another base class.

EXAMPLE:

// C++ program to implement
// Multilevel Inheritance
#include
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"Objects with 4 wheels are vehicles"< }
};
// sub class derived from two base classes
class Car: public fourWheeler{
public:
car()
{
cout<<"Car has 4 Wheels"< }
};
// main function
int main()
{
//creating object of the subclass will
//invoke the constructor of base classes
Car obj;
return 0;
}
output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels

4) HIERARCHICAL INHERITANCE:

In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class.

EXAMPLE:

// C++ program to implement
// Hierarchical Inheritance
#include
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// first sub class
class Car: public Vehicle
{
};
// second sub class
class Bus: public Vehicle
{
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}
Output:
This is a Vehicle
This is a Vehicle

5) HYBRID INHERITANCE:

Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.

EXAMPLE:

// C++ program for Hybrid Inheritance
#include
using namespace std;
// base class
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
//base class
class Fare
{
public:
Fare()
{
cout<<"Fare of Vehiclen";
}
};
// first sub class
class Car: public Vehicle
{
};
// second sub class
class Bus: public Vehicle, public Fare
{
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Bus obj2;
return 0;
}
Output:
This is a Vehicle
Fare of Vehicle

Syntax:

class subclass_name : access_mode base_class
{
//body of subclass
};

EXAMPLE:

// C++ program to explain
// Single inheritance
#include
using namespace std;
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
// sub class derived from two base classes
class Car: public Vehicle{
};
// main function
int main()
{
// creating object of subclass will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output:
This is a vehicle

4) DATA ABSTRACTION:

Data abstraction means showcasing only the required things to the outside world while hiding the details. Basically, data abstraction is done to hide the complexity of the program.

Example: When we drive a car and press the accelerator to increase the speed or press the brakes to stop the car even without knowing how the accelerator or the brakes work as the driver is not concerned with the working. This is an example of data abstraction as here we hide the working and providing only the essential part to the user.

Data abstraction helps in various ways such as:

1) Prevent from writing low-level code

2) avoid code duplication and increases reusability.

3) Helps to increase security.

5) DATA ENCAPSULATION:

The process of hiding the implementation details of the class from the user is called as data encapsulation. This an achieved through the private fields and the public methods of classes. Data abstraction is basically bound data variables and functions together in a class.

Data encapsulation is also known as data hiding. Data encapsulation can be achieved by access specifiers which are public, protected, and private. These specifiers help in hiding the data and exposing those only which need to be exposed.

EXAMPLE:

#include
using namespace std;
class Adder {
public:
// constructor
Adder(int i = 0) {
total = i;
}
// interface to outside world
void addNum(int number) {
total += number;
}
// interface to outside world
int getTotal() {
return total;
};
private:
// hidden data from outside world
int total;
};
int main() {
Adder a;
a.addNum(10);
a.addNum(20);
a.addNum(30);
cout << "Total " << a.getTotal() < return 0;
}
When the above code is compiled and executed, it produces the following result ???
Total 60

Above class adds numbers together, and returns the sum. The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that is hidden from the outside world but is needed for the class to operate properly.

6) POLYMORPHISM:

The word polymorphism means having many forms. Polymorphism is a concept which allows us to redefine the way something works, by either changing how it is done or by changing the parts using which it is done.

It is a feature which let us create functions with same name but different arguments, which will perform different actions.

Send a Message
WhatsApp chat
Enquiry Now

Please fill up the form below and our advisors will get in touch you.