0% found this document useful (0 votes)
179 views5 pages

Object Oriented Programming PDF

Class is the blueprint for objects in object-oriented programming. A class defines properties and behaviors that objects of that class will have. An object is an instance of a class that allocates memory. C++ supports primitive data types like int and float, as well as user-defined types like classes, structures, unions, and namespaces to organize code. Arrays allow storing multiple values of the same type in a single variable.

Uploaded by

Arivu Anand
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
179 views5 pages

Object Oriented Programming PDF

Class is the blueprint for objects in object-oriented programming. A class defines properties and behaviors that objects of that class will have. An object is an instance of a class that allocates memory. C++ supports primitive data types like int and float, as well as user-defined types like classes, structures, unions, and namespaces to organize code. Arrays allow storing multiple values of the same type in a single variable.

Uploaded by

Arivu Anand
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Object Oriented Programming

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

Floating point float

Double floating point double

Valueless void

Wide character wchar_t

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

char 1byte -127 to 127 or 0 to 255


unsigned char 1byte 0 to 255

signed char 1byte -127 to 127

int 4bytes -2147483648 to 2147483647

unsigned int 4bytes 0 to 4294967295

signed int 4bytes -2147483648 to 2147483647

short int 2bytes -32768 to 32767

unsigned short int 2bytes 0 to 65,535

signed short int 2bytes -32768 to 32767

long int 8bytes -2,147,483,648 to 2,147,483,647

signed long int 8bytes same as long int

unsigned long int 8bytes 0 to 4,294,967,295

long long int 8bytes -(2^63) to (2^63)-1

unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615

float 4bytes

double 8bytes

long double 12bytes

wchar_t 2 or 4 bytes 1 wide character

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"};

To create an array of three integers, you could write:


int myNum[3] = {10, 20, 30};

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};


cars[0] = "Opel";
cout << cars[0];
structure is another user defined data type which allows you to combine data items of different kinds.
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Example:
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;

#include <iostream>
#include <cstring>

using namespace std;

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;

// Print Book1 info


cout << "Book 1 title : " << Book1.title <<endl;
cout << "Book 1 author : " << Book1.author <<endl;
cout << "Book 1 subject : " << Book1.subject <<endl;
cout << "Book 1 id : " << Book1.book_id <<endl;

// Print Book2 info


cout << "Book 2 title : " << Book2.title <<endl;
cout << "Book 2 author : " << Book2.author <<endl;
cout << "Book 2 subject : " << Book2.subject <<endl;
cout << "Book 2 id : " << Book2.book_id <<endl;

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

You might also like