Unit 2 - Summary 1
Unit 2 - Summary 1
Unit 2 - Summary 1
C and C++
C - procedure-oriented
/*C-like comments*/
C++ - object-oriented or object-based
// is useful for a single-line comment
/*C-like comments is supported in C++*/
Limitations of C
used in small- and medium-sized programs
It lacks a global view, and the programming design does not support reusability
Lacks abstraction
Not Designed for Reusability
Object-oriented Programming
Global way of programming
Objects are real-world entities
Class – extension of Struct
process of combining data and function attributes of an entity is known as encapsulation.
Object-oriented programming uses classes and objects with inheritance.
Object-based programming
Object-based programming uses objects and classes but without inheritance.
Operator overloading, function overloading, and the use of constructors and destructors are
included in object-based programming.
C++ does not represent the pure object-oriented model.
Attributes of a class - Data attributes and Function attributes
A programmer needs to model both of them in an object.
Notes:
There is no restriction on the type of function a class can have, but a good design
requires only related functions to be a part of the class.
The thin class design requires only necessary functions and data attributes to be part
of a class, in a way making it easy to administer. When a class has less number of
member functions, it is less affected when the class grows or changes.
Operations are performed by and on objects and not classes.
The functions that are a part of the class are known as member functions. A member
function helps programmers to control its access.
The objects of the class alone can operate on the functions owned by them.
Class is a data type and object is a variable of that type. A single class may have
multiple objects associated with it.
Classes contain attributes that all the objects share.
When the objects of a class behave like a data type, the class is known as an abstract
data type (ADT).
A member function is invoked similar to a data member with the dot operator after the
object name.
Abstraction hides unnecessary things and reveals only those that the user needs to
manipulate. Proper abstraction can make a complex and large problem a whole lot
simpler and manageable.
The process of designing a class with some attributes hidden and some exposed is
known as encapsulation.
A friend function can access and manipulate the private entities of a class.
An abstract class is a class with no objects - pure virtual function in a class to make it
abstract
polymorphism is the ability of a single object to appear in many forms.
different functions with the same name (Add) but with either different sets of
arguments or different number of arguments – ex. operator overloading
inheritance and run-time polymorphism are applicable only to object-oriented
programming and not to object-based programming.
INFORMATION HIDING A class should make the required functions available and
hide the other data and functions. This process helps the user to have an uncluttered
view and is useful to have objects as an abstraction. This is the principle of
information hiding
STL- Standard Template Library
class Student
{
int rollno; char name[20];
/* A constructor function, bearing the same name as the class */
Student()
{
roll no = 0;
}
};
Student st1; /* Defining an object of class Student, notice the use of class name 'Student'
instead of 'class Student' as a type */
st1.rollno is initialized to zero automatically when the statement Student st1; gets executed.
function Student() is defi ned inside the class Student
The advantage is that we do not need to explicitly initialize the roll number to zero when we
defi ne an object of student.
Suppose we define 100 students or an array of students. Each time the constructor function is
automatically called, the roll number for all those students is initialized to zero.
Operator Overloading
complex c1, c2;
C++ keywords
Pointer definitions in C++ operate just like those in C, with the exception of void pointers and
two new types of pointers, the constant pointer and pointer to constant.
C++ is a strictly typed language.
A constant pointer is one that cannot point to anything other than what it is pointing to at the
time of its definition.
When the constant pointer is used, the address itself cannot be changed; however, it must be
noted that the content of that address can be changed.
A pointer to constant is a pointer variable that can point to any memory location, but the
content of the memory location to which it points cannot be modified. Thus, if one defines int
const* SecondPointer = &Content; it is not possible to modify Content using SecondPointer.
Hence, the statement *SecondPointer = 100; will now become invalid
If a function that returns a normal variable is used on the LHS, the C++ compiler will fl ag an
error indicating ‘lvalue required’ (lvalue refers to the LHS value of an assignment). This error
indicates that the LHS value of an assignment statement must be a variable to which the value
of the RHS expression can be assigned
When a function returns a reference, it can be used on the LHS of an assignment statement.
The function, in this case, should return only a global variable or a variable local to the calling
function and this variable is assigned the RHS value. A function cannot use a variable local to
the scope of the function as a returning value.
The scope resolution operator makes it possible for a programmer to use both global and local
variables with the same name..