Object Oriented Programming: Chapter 1: Principles of Object Oriented Programming

Data types

C++ Programming

Questions: 1. Describe data types in C++ in detail. 2. What is user defined data types? What is the scope and life time of variable ? Explain using C++ program. 3. Describe the concept of enumerated data Type in C++ with a proper example.

Data‒types

• Definition: Data types are used to define the type of data for particular variable. These are specified using specific keyword.

There are three types of data types ‒

1. Fundamental or Primitive or Basic Data types

2. Derived Data types

3. User defined Data types


Data type

Primitive

Derived

User‒defined

 

1.Basic Data Types

The built‒in data types supported by C++ are enlisted in the following table.


 

2. Derived Data Types

These are the data types that are derived or created from the fundamental data types.

In C++ arrays and strings are considered as derived data types.

Syntax of Arrays

Data_type array_name[size];

Example of Arrays

int a[10];

Syntax of Strings

char array_name[size];

Example of strings

char msg[]=('h','e','l','l','o'};


3. User‒Defined Data Types

• User defined data types are the data types that allow to store multiple values either of same data type or different data type or both.

• In C++ user defined data types are denoted using struct, union or enum.

Enum

• To create user defined type names the keyword enum is used. The syntax of using enum is

enum name{list of names) variables;

For example:

enum day{Monday,Tuesday,Wednesday} d;

d=Tuesday;

• By default the value of first name is 0, second name has value 1 and so on. But we can give some other specific value to the list element. For example

enum day{Monday=10,Tuesday=20,Wednesday=30};

• Following is a simple C++ program that illustrates the use of enum

/***********************************

Program for using enum

**********************************/

#include<iostream>

using namespace std;

int main()

{

       enum mylist{small,middle=5,large} a,b;

        a=middle;

       b=large;

       cout<<"a= "<<a<" b= "<<b;

       return 0;

}

Output

a= 5 b=6

Struct

•  Structure in C is a programming construct that keeps together the variables that need to be in one entity.

•  Structure is a basically the collection of the data members that belong to different data types.

• The keyword struct is used to define the structure in C.

(1) Syntax of C Structure

struct name_of_structure

{

data type member1;

data type member2;

data type member3;

...

data type membern

};

(2) Example of Structure

struct student

{

       int rollNo;

       char name[20];

       float percentage;

}:

 (3) Accessing Structure Members

We can access the structure member using the dot operator. For example

struct student

{

       int rollNo;

       char name[20];

       float percentage;

}s1,s2;

s1.rollNo=10;

strcpy(s1.name, "AAA");

Here s1 and s2 are the variables of structure using which we can access the members of structure with the help of dot operator.

 

Example 1: An unsigned int can be twice as large as the signed int. Explain how?

Solution :

1) The sign needs one bit, and the rest are used for the integer.

2) Unsigned uses this bit also as part of the integer. And as one extra bit available is equivalent to doubling the capacity.

3) For instance: Let us assume that the machine is 32 bit wide. For a signed integer, when 32 bits are allocated, 1 bit has to be used for storing the sign information. So the actual number can only be 31 bit wide.

For an unsigned integer, the whole 32 bits can be used.

So the range of signed int will be ‒ 32768 to 32768 and the range of unsigned int will be 0 to 65,535.

 

Review Questions

1. Describe data types in C++ in detail.

2. What is user defined data types? What is the scope and life time of variable ? Explain using C++ program.     

3. Describe the concept of enumerated data Type in C++ with a proper example.


Object Oriented Programming: Chapter 1: Principles of Object Oriented Programming : Tag: Oops, Computer Programming : C++ Programming - Data types


Object Oriented Programming: Chapter 1: Principles of Object Oriented Programming



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation