Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ vs. C: A Comparative Analysis of Programming Languages - Prof. Rose, Lecture notes of Computer Programming

This document offers a comprehensive comparison of c and c++, highlighting key differences in syntax, features, and applications. it delves into data types, memory management, object-oriented programming concepts supported by c++, and explores real-world applications of both languages in operating systems, embedded systems, and more. Valuable for students learning programming languages and those seeking to understand the evolution and distinctions between c and its object-oriented extension, c++. it provides a solid foundation for further study in computer science.

Typology: Lecture notes

2021/2022

Available from 05/10/2025

r22syedamubarrahmpcs029
r22syedamubarrahmpcs029 🇮🇳

5 documents

1 / 35

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT I Chpt I Introduction to C++
Enlisted below are the major set of differences between the programming languages, C
and C++.
Sl.No
Basis Of
Distinction
C
C++
1
Nature Of
Language
C is a structural or
procedural type of
programming
language (POP).
C++ is an object-oriented
programming(OOP) language and supports
Polymorphism, Abstract Data Types,
Encapsulation, among others. Even though
C++ derives basic syntax from C, it cannot
be classified as a structural or a procedural
language.
2
Point Of
Emphasis
C lays emphasis on
the steps or
procedures that are
followed to solve a
problem.
C++ emphasizes the objects and not the
steps or procedures. It has higher
abstraction level.
3
Compatibility
With
Overloading
C does not support
function overloading.
C++ supports function overloading,
implying that one can have name of
functions with varying parameters.
4
Data Types
C does not provide
String data types.
C++ provides String data types.
5
Compatibility
With
Exception
Handling
C does not support
Exception Handling
directly. It can be
done through some
other functions.
C++ supports Exception Exception.
Handling can be done through try & catch
block.
6
Compatibility
With Generic
Programming
C is not compatible
C++ is compatible with generic
programming
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23

Partial preview of the text

Download C++ vs. C: A Comparative Analysis of Programming Languages - Prof. Rose and more Lecture notes Computer Programming in PDF only on Docsity!

UNIT I – Chpt I – Introduction to C++

Enlisted below are the major set of differences between the programming languages, C and C++. Sl.No Basis Of Distinction

C C++

Nature Of Language C is a structural or procedural type of programming language (POP). C++ is an object-oriented programming(OOP) language and supports Polymorphism, Abstract Data Types, Encapsulation, among others. Even though C++ derives basic syntax from C, it cannot be classified as a structural or a procedural language. 2 Point Of Emphasis C lays emphasis on the steps or procedures that are followed to solve a problem. C++ emphasizes the objects and not the steps or procedures. It has higher abstraction level. 3 Compatibility With Overloading C does not support function overloading. C++ supports function overloading, implying that one can have name of functions with varying parameters. 4 Data Types C does not provide String data types. C++ provides String data types. 5 Compatibility With Exception Handling C does not support Exception Handling directly. It can be done through some other functions. C++ supports Exception Exception. Handling can be done through try & catch block. 6 Compatibility With Generic Programming C is not compatible C++ is compatible with generic programming

Pointers And References C supports only Pointers C++ supports both pointers and references. 8 Data Security In C programming language, the data is unsecured. Data is hidden in C++ and is not accessible to external functions. Hence, is more secure 9 Approach C follows the top- down approach. C++ follows the bottom-up approach. 10 Functions For Standard Input And Output scanf and printf cin and cout 11 Time Of Defining Variables In C, variable has to be defined at the beginning, in the function. Variable can be defined anywhere in the function. (^12) Namespace Absent Present 13 Division Of Programs The programs in C language are divided into modules and functions. The programs are divided into classes and functions in the C++ programming language. 14 File Extension .c .cpp 15 Function And Operator Overloading Absent Present 16 Mapping Mapping between function and data is Mapping between function and data can be done easily using ‘Objects’.

Uses of C++

C++ is used by programmers to create computer software. It is used to create general systems software , drivers for various computer devices, software for servers and software for specific applications and also widely used in the creation of video games. C++ is used by many programmers of different types and coming from different fields. C++ is mostly used to write device driver programs , system software and applications that depend on direct hardware manipulation under real time constraints. It is also used to teach the basics of object oriented features because it is simple and is also used in the fields of research. Also, many primary user interfaces and system files of Windows and Macintosh are written using C++. So, C++ is really a popular, strong and frequently used programming language of this modern programming era.

Features of C++

  1. Better memory management – you can dynamically allocate memory during runtime using new and delete operator in C++ to have better memory management.
  2. Object oriented – C++ supports object oriented programming features, which means we can use the popular OOPs concepts such as Abstraction, Polymorphism, Encapsulation and Inheritance in C++ programs, these features make writing code in C++ a lot easier.
  3. Portable – Most of C++ compilers supports ANSI standards that makes C++ portable because the code you write on one operating system can be run on other Operating system without making any change. We cannot say C++ a fully platform independent language as certain things in C++ are not portable, such as drawing graphics on a screen, since standard C++ has no graphics or GUI API.
  4. Structured programming language – We have functions in C++, which makes easier to break a problem into small blocks of code and structure the program in such a way so that it improves readability and reusability.
  5. Exception handling : Just like Java we can do exception handling in C++ which makes it easier to identify and handle the exceptions.
  6. Simple – Last but not least, just like C, it is easier to write a program in C++. Once you get familiar with the syntax of C++ programming language, it becomes a lot easier to code in C++.

Example 1: Hello World Program #include using namespace std; int main() { cout << "Hello, World!"; return 0 ; } Output Hello, World! Every C++ program starts from the main() function. The cout is the standard output stream which prints the "Hello, World!" string on the monitor. The return 0; is the Exit status" of the program. Example 2: Hello World Program #include int main() { std::cout << "Hello, World!"; return 0 ; } Output Hello, World! What does 'using namespace std' mean in C++? Namespaces are like drawers in a filing cabinet. The entire standard library is in one drawer, and the only way to get to the stuff in that drawer is to tell C++ where to find it. You do that by prefixing the name with std:

4. Embedded Systems: Various features of C including direct access to machine level hardware APIs, presence of C compilers, deterministic resource use and dynamic memory allocation make C language an optimum choice for scripting applications and drivers of embedded systems. 5. Graphics and Games: C language has been used in the development of a variety of graphics and gaming applications, such as chess, bouncing ball, archery etc. **Real-World Applications of C++

  1. Games:** C++ overrides the complexities of 3D games, optimizes resource management and facilitates multiplayer with networking. The language is extremely fast, allows procedural programming for CPU intensive functions and provides greater control over hardware, because of which it has been widely used in development of gaming engines. For instance, the science fiction game Doom 3 is cited as an example of a game that used C++ well and the Unreal Engine, a suite of game development tools, is written in C++. 2. Graphic User Interface (GUI) based applications: Many highly used applications, such as Image Ready, Adobe Premier, Photoshop and Illustrator, are scripted in C++. 3. Web Browsers: With the introduction of specialized languages such as PHP and Java, the adoption of C++ is limited for scripting of websites and web applications. However, where speed and reliability are required, C++ is still preferred. For instance, a part of Google’s back-end is coded in C++, and the rendering engine of a few open source projects, such as web browser Mozilla Firefox and email client Mozilla Thunderbird, are also scripted in the programming language. 4. Advance Computations and Graphics: C++ provides the means for building applications requiring real-time physical simulations, high-performance image processing, and mobile sensor applications. Maya 3D software, used for integrated 3D modeling, visual effects and animation, is coded in C++. 5. Database Software:

C++ and C have been used for scripting MySQL, one of the most popular database management software. The software forms the backbone of a variety of database-based enterprises, such as Google, Wikipedia, Yahoo and YouTube etc.

6. Operating Systems: C++ forms an integral part of many of the prevalent operating systems including Apple’s OS X and various versions of Microsoft Windows, and the erstwhile Symbian mobile OS. 7. Enterprise Software: C++ finds a purpose in banking and trading enterprise applications, such as those deployed by Bloomberg and Reuters. It is also used in development of advanced software, such as flight simulators and radar processing. 8. Medical and Engineering Applications: Many advanced medical equipments, such as MRI machines, use C++ language for scripting their software. It is also part of engineering applications, such as high-end CAD/CAM systems. 9. Compilers: A host of compilers including Apple C++, Bloodshed Dev-C++, Clang C++ and MINGW make use of C++ language. C and its successor C++ are leveraged for diverse software and platform development requirements, from operating systems to graphic designing applications.

The Parts of a C++ Program

cout << "Programming is" << endl; cout << "fun!";

  • You do NOT put quotation marks around endl
  • The last character in endl is a lowercase L, not the number 1.

C++ Tokens

C++ Tokens are the smallest individual units of a program. Following are the C++ tokens : (most of c++ tokens are basically similar to the C tokens)  Keywords  Identifiers  Constants  Variables  Operators  Strings Keywords Keywords is reserved words which have fixed meaning and its meaning cannot be changed. Identifiers

Identifiers refers to the name of variables, functions, arrays, classes, etc. created by the user. Identifiers are the fundamental requirement of any language. Identifier naming conventions  Only alphabetic characters, digits and underscores are permitted.  First letter must be an alphabet or underscore (_).  Identifiers are case sensitive.  Reserved keywords cannot be used as an identifier's name. Constants Constants refers to fixed values that do not change during the execution of a program. Declaration of a constant : const [data_type] [constant_name]=[value]; Variable A variable is a meaningful name of data storage location in computer memory. When using a variable you refer to memory address of computer. We know that in C, all variables must be declared before they are used, this is true with C++. The main difference in C and C++ with regards to the place of their declaration in the program... C requires all the variables to be defined in the beginning of scope. C++ allows the declaration of a variable anywhere in the scope, this means that a variable can be declared right at the place of its first use. Syntax to declare a variable : [data_type] [variable_name]; Consider the example #include <iostream.h> int main()

++ Increment −− Decrement

Relational Operators

Operator Description == Is equal to != Is not equal to

Greater than < Less than = Greater than or equal to <= Less than or equal to

Logical Operators

Operator Description && (^) And operator. Performs a logical conjunction on two expressions. (if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False) || Or operator. Performs a logical disjunction on two expressions. (if either or both expressions evaluate to True, result is True) ! (^) Not operator. Performs logical negation on an expression.

Bitwise Operators

Operator Description << Binary Left Shift Operator

Binary Right Shift Operator

~ Binary Ones Complement Operator & Binary AND Operator ^ Binary XOR Operator | Binary OR Operator

Assignment Operators

Operator Description = Assign += Increments, then assigns

  • = Decrements, then assigns *= Multiplies, then assigns /= Divides, then assigns %= Modulus, then assigns <<= Left shift and assigns

= Right shift and assigns &= Bitwise AND assigns ^= Bitwise exclusive OR and assigns |= Bitwise inclusive OR and assigns

Misc Operators

Operator Description , Comma operator sizeof() Returns the size of an memory location. & Returns the address of an memory location.

Data Types available in C++:

  1. Primary(Built-in) Data Types: o character o integer o floating point o boolean o double floating point o void o wide character
  2. User Defined Data Types: o Structure o Union o Class o Enumeration
  3. Derived Data Types: o Array o Function o Pointer o Reference Both C and C++ compilers support the fundamental, i.e., the built-in data types. Taking void as an exception the basic data types may have several modifiers, and these modifiers are used to serve the data types in various situations. The lists of modifiers used in C++ are:  signed  unsigned  long  short

Character Data Types

Data Type (Keywords) Description Size Typical Range char Any single character. It may include a letter, a digit, a punctuation mark, or a space. 1 byte - 128 to 127 or 0 to 255 signed char Signed character. 1 byte - 128 to 127 unsigned char Unsigned character. 1 byte 0 to 255 w char_t Wide character. 2 or 4 bytes 1 wide character

Integer Data Types

Data Type (Keywords) Description Size Typical Range int Integer. 4 bytes - 2147483648 to 2147483647 signed int Signed integer. Values may be negative, positive, or zero. 4 bytes - 2147483648 to 2147483647 unsigned int Unsigned integer. Values are always positive or zero. Never negative. 4 bytes 0 to 4294967295 short Short integer. 2 bytes - 32768 to 32767 signed short Signed short integer. Values may be negative, positive, or zero. 2 bytes - 32768 to 32767 unsigned short Unsigned short integer. Values are always positive or zero. Never negative. 2 bytes 0 to 65535 long Long integer. 4 bytes - 2147483648 to 2147483647 signed long Signed long integer. Values may be negative, positive, or zero. 4 bytes - 2147483648 to 2147483647 unsigned long Unsigned long integer. Values are always positive or zero. Never negative. 4 bytes 0 to 4294967295 Floating-point Data Types Data Type (Keywords) Description Size Typical Range float Floating point number. There is no fixed number of digits before or after the decimal point. 4 bytes +/- 3.4e +/- 38 (~7 digits) double Double precision floating point number. More accurate compared to float. 8 bytes +/- 1.7e +/- 308 (~15 digits) long double Long double precision floating point number. 8 bytes +/- 1.7e +/- 308 (~15 digits) Boolean Data Type Data Type Description Size Typical

Example: The cin Object Reads Different Data Types

Precedence & Associativity of Operators

Definitions:

  • Expression: An expression is a sequence of operands and operators that reduce to a single value.
  • Precedence: Precedence is used to determine the order in which different operators in a complex expression are evaluated.
  • Associativity: Associativity is used to determine the order in which operators with the same precedence are evaluated in a complex expression.

Type Casting

  • Used for manual data type conversion
  • Useful for floating point division using ints: double m; m = static_cast(y2-y1)/(x2-x1);
  • Useful to see int value of a char variable: char ch = 'C'; cout << ch << " is " << static_cast(ch);

Stream Manipulators

  • Used to control how an output field is displayed
  • Some affect just the next value displayed:
    • setw(x): print in a field at least x spaces wide. Use more spaces if field is not wide enough
  • Some affect values until changed again:
    • fixed: use decimal notation for floating-point values
    • setprecision(x): when used with fixed, print floating-point value using x digits after the decimal. Without fixed, print floating-point value using x significant digits
    • showpoint: always print decimal for floating-point values