Object Oriented Programming PDF
Object Oriented Programming PDF
Class:
The building block of C++ that leads to Object-Oriented programming is a Class.
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 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.
Object:
An Object is an identifiable entity with some characteristics and behavior.
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.
class person
{
char name[20];
int id;
public:
void getdetails(){}
};
int main()
{
person p1; // p1 is a object
}
Primitive Built-in Types
C++ offers the programmer a rich assortment of built-in as well as user defined data types. Following
table lists down seven basic C++ data types −
Type Keyword
Boolean bool
Character char
Integer int
Valueless void
Several of the basic types can be modified using one or more of these type modifiers −
signed
unsigned
short
long
The following table shows the variable type, how much memory it takes to store the value in memory,
and what is maximum and minimum value which can be stored in such type of variables.
Type Typical Bit Width Typical Range
float 4bytes
double 8bytes
The size of variables might be different from those shown in the above table, depending on the compiler
and the computer you are using.
C++ Arrays
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
To declare an array, define the variable type, specify the name of the array followed by square
brackets and specify the number of elements it should store:
string cars[4];
We have now declared a variable that holds an array of four strings. To insert values to it, we
can use an array literal - place the values in a comma-separated list, inside curly braces:
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
#include <iostream>
#include <cstring>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
When the above code is compiled and executed, it produces the following result −
Book1 title : Learn C++ Programming
Book1 author : Chand Miyan
Book1 subject : C++ Programming
Book1 id : 6495407
Book2 title : Telecom Billing
Book2 author : Yakit Singha
Book2 subject : Telecom
Book2 id : 6495700
A namespace is designed to overcome this difficulty and is used as additional information to differentiate
similar functions, classes, variables etc. with the same name available in different libraries. Using
namespace, you can define the context in which names are defined.
Union:
Union is a user-defined datatype. All the members of union share same memory location. Size of union is
decided by the size of largest member of union. If you want to use same memory location for two or more
members, union is the best for that.
union union_name {
member definition;
} union_variables;
Here,
union_name − Any name given to the union.
member definition − Set of member variables.
union_variable − This is the object of union.
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
} data, data1;
int main( ) {
printf( "Memory size occupied by data : %d\t%d",
sizeof(data), sizeof(data1));
return 0;
}
Here is the output
Memory size occupied by data : 4 4