C++ As Better C
Before moving on further in advanced C++, it is better to ensure us
that C++ is nothing but an advanced form of C. Those of you who came
from Pascal background may have not found learning C difficult as it
was mere syntax change. But when you go to C++ from C, the thinking
process needs to be changed. I have seen many people use just C++
environment and literally code in C only. I request you again to
understand below features which were added to C++ as a solution to make
code more efficient. The best way to learn a thing is to put it to
practice as soon as you need it. So, here is your revision.
- Programming Paradigms
* Procedural Programming Top-Down Approach
* Object-Based Programming - Classes, Objects, Encapsulation,Operator Overloading BUT NO INHERITANCE e.g. Visual Basic
* Object-Oriented Programming - Above + Inheritance, Polymorphism e.g. VC++
* Generic Programming - Function Templates, Class Templates
* Another Aspect - event-driven programming, graphics programming, GUI & multimedia programming
- Introduction to C++
* Developed by Bjarne Stroustrup at Bell Labs
* C++ = C with Classes
- Difference between C & C++
* C is procedural and does not support object-oriented features (PIE)
* C does not have call-by-reference
* C does not have single line comment
* C++ does not allow omission of function return-vale type
* C does not have new and delete
* C++ has better I/O.. cout, cin, stream insertion extraction operators can be
used for own data-types (operator overloading)
* In C++, variables can be declared anywhere before their use
* C++ is enhanced version of C. So, C++ compiler will compile C program but not vice-versa.
- Advantages of OOPs
* Complexity Management
Through Abstraction, Encapsulation, and Information Hiding. No Global Variables.
* Software Reuse (of classes/functions)
Productivity Increases: Development time is reduced as no need to define own classes.
Quality Increases: Re-usable software are efficiently & well designed, developed,tested and maintained.
- Library and Header Files
* Each library has a corresponding header file
* Header file contains function prototypes, data-types and constants
Old Style: New Style: "iostream"
* Inline Functions
Increased Program Size Function code is substituted wherever inline function is called
Reduced Execution Time No overhead of function call
Use only for small and frequently used functions
Change in inline function requires all clients to be re-compiled.
* Convention
– std::cout
– using std::cout
– using namespace std
* Keywords: All of C + others
* Passing Arguments to Functions
Call-By-Value: Use for small and non-modifiable arguments
Call-By-Reference:
– Increased Performance =¿ No overhead of coping large amount of data
– Weaker Security =¿ Caller’s data can be corrupted
(a) Using Pointers
(b) using Reference Parameters
– Alias (another name) for corresponding argument
– Pass large data by const reference =¿ Performance + Security
– Prototype: void SqByRef(int &) Call: SqByRef(z) Defn: void SqByRef(int &a)a*=a;
- Reference is obtained by pre-pending the variable name with &
_ By looking at calling, you cannot tell if variable is passed by value or by reference i.e. whether argument will be modified or not.
* More on Reference
– References can also be used as alias for other variables within a function
– References must be initialized in their declarations. Not doing so is a syntax error
int count = 1;
int &cref = count;
– Neither taking the address of a reference nor comparing references cause syntax
errors => Each operation actually occurs on the variable for which reference is an alias.
– A reference argument must be an lvalue and not a constant or expression that returns an rvalue.
int &x=a,y=b,z=c;
int &x,y,z; //wrong
++cref;
– Returning a pointer or reference to an automatic variable in a called function is a logical error i.e.compiler may WARN. References to undefined variables are called Dangling References.
* Empty parameter list disables checking. C not equal to C++
* Default Arguments
– Simple but not clean approach
– If ommitted then auto-inserted by compiler & and passed in call.
– Must be rightmost (trailing) arguments in function parameter list
– If XXX then others should also be omitted
* Unary Scope Resolution Operator
Used for local and global variables with the same name
* Parameterized Stream Manipulator
#include
setprecision(20)-- after decimal places
setw(28)
setiosflag (ios::fixed|ios::showpoint) - Inclusive OR
* Function Overloading:
Used to create several functions of the same name that perform similar tasks on different data types. Should differ in numbers, types and orderof arguments in call. Different only in return types is not function overloading - It issyntax error Operator Overloading/Function Templates = auto generating overloading functions that perform identical tasks on different data types signatures (function name+parameter type)-name mangling or name decoration to enable type-safe linkage- ensures that the proper overloaded function is called and the arguments conform toparameters. Linkage errors are detected and reported by the compiler. Overloadedfunctions with default parameters may cause ambiguity.
* Function Templates
Enable creation of functions that perform the same (identical)
operations on different types of data but the function template is defined only once.
C++ automatically generates separate template function to handle each type of call
appropriately.
template
T maximum(T i, T j)
{
int max;
i>j?max=i:max=j;
return max;
}
Formal type parameters are built-in types or user-defined types used to specify the
types of the arguments to the function, to specify return type of function and todeclare variables within the body of the function definition.
* Class = Structure + Function
– Structures: Group data elements
– Functions: Organize program actions into named entities
– Classes: Group data elements + Organize program actions into named entities
* Role of Variable Type
– Size
– Information it can hold
– Supported operations
* Creating New Types
– C++ allows creating new types as powerful as built-in types
– Why new types: Closer rep resentation of real world makes writing programs easier e.g. heating systems have variables like rooms, heat sensors
In next post, we will see in detail classes with respect to data abstraction.
Source: C++ - How To Program by Deitel & Deitel
Page Author
From Here You Can…
Information
- 1148 Views
- 0 Comments
Most Recent Related Content
- Lesson
- Avatar

- Title
- What's Wrong With This Picture?
- Body
- (cc) Roger Jones For too long learners have been let down by the way they...
- Author
- Lesson
- Avatar

- Title
- Opportunity Knocking ... Anybody Home?
- Body
- I’m having a tough time making any kind of sense of this, myself…...
- Author
- Lesson
- Avatar

- Title
- LIVE: Sell Your Expertise in Online Knowledge Marketplaces
- Body
- photo credit: e-magicOkay… You asked. I listened. I’ve set...
- Author
- Lesson
- Avatar

- Title
- Intellectual Property - The Comic Book
- Body
- If the whole idea of intellectual property and fair use makes your head hurt,...
- Author
- Lesson
- Avatar

- Title
- The WebQuest
- Body
- Computers and the Internet are valuable teaching and learning tools. Find out...
- Author
- Lesson
- Avatar

- Title
- What Works: Giving Feedback to Learners
- Body
- Like it or not, certain ways of giving learners feedback HELP them learn.Cert...
- Author
- Lesson
- Avatar

- Title
- Using Aplications made for Windows in Ubuntu
- Body
- Did you know that you can run lots programs available to windows in ub...
- Author
- Lesson
- Avatar

- Title
- All about Identifiers in C++
- Body
- In our everyday, we give names to different things so they can be referred ea...
- Author
Published In…
Computer Science
Software Carpentry
Java
Agile Software Development
Everything PHP
Web 2.0
Technical Writing, Training, Publishing
Open Source
tech
Game Development
Computer Basics
PHP and MySQL
Google
J2EE
Computer Hardware
GNU & Linux
Social Media
C++
Programming Languages
Software Architecture
Computer Science Learners
This work is public domain.