Unit-1 OOP_DS
Unit-1 OOP_DS
1. Primitive Data Types: These data types are built-in or predefined data
types and can be used directly by the user to declare variables. example:
int, char , float, bool etc. Primitive data types available in C++ are:
Integer
Character
Boolean
Floating Point
Double Floating Point
Valueless or Void
Wide Character
2. Derived Data Types: The data-types that are derived from the primitive
or built-in datatypes are referred to as Derived Data Types. These can be
of four types namely:
Function
Array
Pointer
Reference
3. Abstract or User-Defined Data Types: These data types are defined by
user itself. Like, defining a class in C++ or a structure. C++ provides the
following user-defined datatypes:
Class
Structure
Union
Enumeration
Typedef defined DataType
This article discusses primitive data types available in C++.
Integer: Keyword used for integer data types is int. Integers typically
requires 4 bytes of memory space and ranges from -2147483648 to
2147483647.
Boolean: Boolean data type is used for storing boolean or logical values.
A boolean variable can store either true or false. Keyword used for
boolean data type is bool.
Floating Point: Floating Point data type is used for storing single
precision floating point values or decimal values. Keyword used for
floating point data type is float. Float variables typically requires 4 byte of
memory space.
Double Floating Point: Double Floating Point data type is used for
storing double precision floating point values or decimal values. Keyword
used for double floating point data type is double. Double variables
typically requires 8 byte of memory space.
void: Void means without any value. void datatype represents a valueless
entity. Void data type is used for those function which does not returns a
value.
Wide Character: Wide character data type is also a character data type
but this data type has size greater than the normal 8-bit datatype.
Represented by wchar_t. It is generally 2 or 4 bytes long.
Concepts of String:
C++ provides following two types of string representations −
Actually, you do not place the null character at the end of a string constant. The
C++ compiler automatically places the '\0' at the end of the string when it initializes
the array. Let us try to print above-mentioned string −
Live Demo
#include <iostream>
int main () {
return 0;
}
When the above code is compiled and executed, it produces the following result −
Greeting message: Hello
C++ supports a wide range of functions that manipulate null-terminated strings −
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
int main () {
return 0;
}
When the above code is compiled and executed, it produces result something as
follows −
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
int main () {
return 0;
}
When the above code is compiled and executed, it produces result something as
follows −
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10
C++ Classes and Objects
Class: A class in C++ is the building block, that leads to Object-Oriented
programming. It is a user-defined data type, which holds its own data
members and member functions, which can be accessed and used by
creating an instance of that class. A C++ class is like a blueprint for an
object.
For Example: Consider the Class of Cars. There may be many cars with
different names and brand 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.
A Class is a user defined data-type which has data members and member
functions.
Data members are the data variables and member functions are the
functions used to manipulate these variables and together these data
members and member functions defines the properties and behavior of
the objects in a Class.
In the above example of class Car, the data member will be speed
limit, mileage etc and member functions can be apply brakes, increase
speed etc.
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.
Defining Class and Declaring Objects
A class is defined in C++ using keyword class followed by the name of class.
The body of class is defined inside the curly brackets and terminated by a
semicolon at the end.
Declaring Objects: When a class is defined, only the specification for the
object is defined; no memory or storage is allocated. To use the data and
access functions defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;
Accessing data members and member functions: The data members and
member functions of class can be accessed using the dot(‘.’) operator with
the object. For example if the name of object is obj and you want to access
the member function with the name printName() then you will have to
write obj.printName() .
Accessing Data Members
The public data members are also accessed in the same way given however
the private data members are not allowed to be accessed directly by the
object. Accessing a data member depends solely on the access control of
that data member.
This access control is given by Access modifiers in C++. There are three
access modifiers : public, private and protected.
Member Functions in Classes
There are 2 ways to define a member function:
Inside class definition
Outside class definition
To define a member function outside the class definition we have to use the
scope resolution :: operator along with class name and function name.