Set of Values TRB CSI › Programming with C++ › Discusses the properties of types and object

Set of Values

Programming Language-ல் Type என்பது ஒரு data item எந்த வகையான value-ஐ வைத்திருக்கலாம், அதற்கு என்ன operations செய்யலாம் என்பதைக் குறிப்பிடுகிறது. Object என்பது program execution-ல் memory-ல் உருவாக்கப்படும் ஒரு data entity. Simple idea: Ty…

Free Beginner 23 min 128 cards 22 programs 8 MCQs v2

PROPERTIES OF TYPES AND OBJECTS

Definition
Meaning

Programming Language-ல் Type என்பது ஒரு data item எந்த வகையான value-ஐ வைத்திருக்கலாம், அதற்கு என்ன operations செய்யலாம் என்பதைக் குறிப்பிடுகிறது.

Object என்பது program execution-ல் memory-ல் உருவாக்கப்படும் ஒரு data entity.

Type = Rule / Category
Object = அந்த type-ஐ சேர்ந்த actual data stored in memory
Explanation
Example
Core idea

int age = 25;

இதில்
  • int → Type
  • age → Object / Variable
  • 25 → Value
  • age-க்கு memory location இருக்கும்
Simple Explanation
Core idea

ஒரு real-life example எடுத்துக்கொள்வோம்.

Detail 01

Type = "Car" என்ற category

Car என்ற type-க்கு சில properties இருக்கும்
  • wheels இருக்கும்
  • engine இருக்கும்
  • speed இருக்கலாம்
  • start/stop செய்யலாம்
Detail 03

ஆனால்

Detail 04

Object = ஒரு particular car

Explanation
Example
Core idea
  1. Car Type
  2. +--- Car 1+--- Car 2+--- Car 3
Detail 01

Programming-லும் அதே concept.

Detail 02
int a = 10;
int b = 20;
Detail 03

இங்கு:

Program
C++ 1 lines
1Type = int
Explanation
Core idea
  • Objects:
  • a
  • b
Detail 01

a, b இரண்டும் different objects. ஆனால் இரண்டின் type ஒன்றே.

Flow
  1. Data
  2. Type is selected
  3. Object is created
  4. Memory is allocated
  5. Value is stored
  6. Operations are performed
  7. Object lifetime ends
  8. Memory is released
Explanation
Example
Core idea

int x = 10;

Flow
  1. int
  2. x object created
  3. memory allocated
  4. 10 stored
  5. x can participate in integer operations
List
  1. WHAT IS A TYPE?
Explanation
Core idea

Type என்பது ஒரு value-ன் nature and behaviour-ஐ define செய்கிறது.

Detail 01

int x = 10;
float y = 10.5;
char ch = 'A';
bool result = true;

Detail 02

இங்கு:

Table
ObjectTypeValue
xint10
yfloat10.5
chchar'A'
resultbooltrue
Explanation
ஒரு type பொதுவாக கீழ்கண்ட விஷயங்களை determine செய்கிறது
  • என்ன values store செய்யலாம்
  • எவ்வளவு memory தேவை
  • எந்த operations allowed
  • values எப்படி represent செய்யப்படும்
  • type checking எப்படி நடக்கும்

Set of Values

Explanation
Core idea

ஒவ்வொரு type-க்கும் அது represent செய்யக்கூடிய possible values இருக்கும்.

Detail 01

bool x;

bool type-ன் values

true
false

அதனால்
  1. Type
  2. defines
  3. Set of possible values
Detail 04

bool → true, false

Detail 05

char → 'A', 'B', '1', '#', ...

Detail 06

int → ..., -2, -1, 0, 1, 2, ...

Set of Operations

Explanation
Core idea

ஒரு type-க்கு எந்த operations செய்யலாம் என்பதையும் type determine செய்கிறது.

Detail 01
int a = 10;
int b = 5;
Program
C++ 1 lines
1cout << a + b;
Explanation
Core idea

போன்ற arithmetic operations possible.

Integer-க்கு

+
-
*
/
%

String example

string a = "Hello";
string b = "World";

Program
C++ 1 lines
1cout << a + b;
Explanation
Core idea

இங்கு + என்பது string concatenation.

Detail 01

அதனால் operation-ன் behaviour கூட type-ஐ பொறுத்தது.

Representation

Explanation
Core idea

ஒரு value computer memory-ல் எப்படி represent செய்யப்படுகிறது என்பதையும் type specify செய்யும்.

Detail 01

int x = 10;

Detail 02

Computer நேரடியாக decimal 10 store செய்யாது.

Detail 03

Binary representation பயன்படுத்தும்.

Detail 04
  1. 10
  2. 1010
Conceptually
  1. Type
  2. determines
  3. Internal representation

Size

Explanation
Core idea

ஒரு type எவ்வளவு memory பயன்படுத்தும் என்பது முக்கிய property.

Detail 01

C++ example:

Program
C++ 3 lines
1cout << sizeof(int);
2cout << sizeof(char);
3cout << sizeof(double);
Verified output
y = 10
Learning Run Mode — this is the output the author verified, not a live execution.
Explanation
Core idea

Exact size implementation/platform-ஐ பொறுத்து மாறலாம்.

Typical implementation-ல்
  • char → 1 byte
  • int → commonly 4 bytes
  • double → commonly 8 bytes
Important

C++ standard எல்லா machines-லும் int = exactly 4 bytes என்று guarantee செய்யாது.

Range

Important
  1. Type எந்த minimum மற்றும் maximum values-ஐ represent செய்ய முடியும் என்பதை range என்கிறோம்.

  2. Example concept

    Type

  3. Size

  4. Number of bits

  5. Possible values

  6. Range

  7. ஒரு 8-bit unsigned value எடுத்தால்

    00000000 → 0

  8. 11111111 → 255

  9. Range

    0 to 255

Precision

Important

Floating-point types-ல் precision முக்கியமான property.

Explanation
Example

float x = 3.1415926;

double y = 3.141592653589793;

Generally

float → lower precision

double → higher precision

Precision என்றால்

ஒரு numeric value எவ்வளவு accurate-ஆக represent செய்யப்படுகிறது.

Type Compatibility

Explanation
Core idea

இரண்டு types ஒன்றுடன் ஒன்று operation செய்ய அல்லது assign செய்ய compatible-ஆக இருக்கிறதா என்பது type compatibility.

Detail 01
int x = 10;
double y = x;
Detail 02

இங்கு integer value double-ஆக convert செய்யப்படுகிறது.

Detail 03
  • int
  • ↓ conversion
  • double
Detail 04

ஆனால் எல்லா types-க்கும் direct compatibility இருக்காது.

Type Conversion

Explanation
Core idea

ஒரு type-லிருந்து மற்றொரு type-க்கு value மாற்றப்படுவது type conversion.

இரண்டு முக்கிய வகைகள்
  1. Type Conversion
  2. +--- Implicit
  3. +--- ExplicitImplicit Conversion
Detail 02

Compiler automatically conversion செய்கிறது.

Detail 03
int x = 10;
double y = x;
Concept
  1. 10int
  2. automatic conversion
  3. 10.0doubleExplicit Conversion

Programmer conversion-ஐ explicitly குறிப்பிடுகிறார்.

double x = 10.8;

int y = static_cast<int>(x);
Output
Result
y = 10

Fractional part lost.

Type Checking

Program-ல் பயன்படுத்தப்படும் types valid-ஆக உள்ளதா என்று checking செய்வது type checking.

Explanation
Example
Core idea

int x = 10;

Detail 01

x integer object.

Invalid conceptual operation

integer + completely incompatible object

Detail 03

Programming language/compiler இதை check செய்யும்.

Detail 04

Static Type Checking

Detail 05

Compile time-ல் type checking நடக்கிறது.

C++ example

int x = 10;

Detail 07

Compiler x-ன் type-ஐ compile time-லேயே அறிந்திருக்கும்.

Detail 08
  1. Source code
  2. Compiler
  3. Type checking
  4. Execution
Detail 09

C, C++, Java போன்ற languages பெரும்பாலும் statically typed.

Detail 10

Dynamic Type Checking

Detail 11

Runtime-ல் type information check செய்யப்படுகிறது.

Detail 12

Python example:

Program
C++ 2 lines
1x = 10
2x = "Python"
Explanation
Core idea

ஒரே name different times-ல் different type objects-ஐ refer செய்யலாம்.

Detail 01

x → int object

Detail 02

later

Detail 03

x → string object

Detail 04

Python dynamically typed language.

Type Equivalence

Explanation
Core idea

இரண்டு types same type-ஆக consider செய்யப்படுகிறதா என்பதே type equivalence.

முக்கிய approaches
  1. Type Equivalence
  2. +--- Name Equivalence
  3. +--- Structural EquivalenceName Equivalence
Detail 02

இரண்டு types same declared type name கொண்டிருந்தால் equivalent.

Concept
Context

Type A
Type B

இரண்டின் structure same இருந்தாலும் names different என்றால் separate types என்று consider செய்யலாம்.

Structural Equivalence

Type names முக்கியமில்லை.

அவற்றின் structure same என்றால் equivalent என்று consider செய்யலாம்.

  • integer
  • character
integer
character
Structure same.
அதனால் structural equivalence approach-ல் equivalent ஆகலாம்.

OBJECT

Definition
Meaning

Object என்பது program execution-ன் போது ஒரு value-ஐ hold செய்யக்கூடிய storage-associated entity.

Memory-ல் இருக்கும் actual data entity தான் Object.

Explanation
Example
Core idea

int marks = 90;

Detail 01

இங்கு:

Program
C++ 4 lines
1Type   = int
2Object = marks
3Value  = 90
4OBJECT ≠ OOP OBJECT ONLY
Explanation
Core idea

இந்த point மிகவும் முக்கியம்.

Detail 01

"Object" என்ற word கேட்டவுடன் C++ class object மட்டும் நினைக்கக்கூடாது.

Programming language theory-ல்

int x = 10;

Detail 03

x கூட ஒரு data object என்று broadly குறிப்பிடப்படலாம்.

OOP context

Student s;

Detail 05

இங்கு s ஒரு class object.

Detail 06

அதனால் context பார்க்க வேண்டும்.

Explanation
Properties of objects
Core idea

ஒவ்வொன்றாக பார்க்கலாம்.

ஒரு object-க்கு பொதுவாக கீழ்கண்ட properties இருக்கும்
  1. Object
  2. +-- Name+-- Type+-- Value+-- Address / Location+-- Size+-- Lifetime+-- Scope / Visibility+-- Mutability+-- Storage duration

NAME

Explanation
Core idea

Object-ஐ program-ல் identify செய்ய பயன்படும் identifier தான் name.

Detail 01

int salary = 50000;

Detail 02

இங்கு:

Program
C++ 1 lines
1salary = object name
Explanation
Core idea

Programmer salary என்ற name மூலம் அந்த object-ஐ access செய்கிறார்.

TYPE

Explanation
Core idea

ஒவ்வொரு object-க்கும் ஒரு type இருக்கும்.

Program
Example
C++ 3 lines
1int age = 25;
2Object = age
3Type   = int
Explanation
Type determine செய்யும்
  • object என்ன values வைத்திருக்கலாம்
  • என்ன operations செய்யலாம்
  • memory representation எப்படி இருக்கும்

VALUE

Explanation
Core idea

Object தற்போது வைத்திருக்கும் information தான் value.

Program
C++ 3 lines
1int x = 10;
2Object = x
3Value  = 10
Program
Later
C++ 1 lines
1x = 20;
Program
இப்போது
C++ 2 lines
1Object = same x
2Value  = 20
Explanation
Core idea

இதனால் object மற்றும் value ஒன்று அல்ல என்பதை புரிந்துகொள்ள வேண்டும்.

Detail 01

VERY IMPORTANT
int x = 10;

Program
C++ 1 lines
1x = 20;
Explanation

x object மாறவில்லை.

அதன் value மட்டும் மாறியது.

Before

x → 10

After

x → 20

MEMORY LOCATION / ADDRESS

Explanation
Core idea

Object பொதுவாக memory-ல் ஒரு location-ஐ occupy செய்கிறது.

Detail 01

int x = 10;

Conceptually

Address Value
1000 10

Detail 03

x அந்த memory location-ஐ identify செய்கிறது.

Detail 04

C++:

Program
C++ 1 lines
1cout << &x;
Explanation
Core idea

&x object x-ன் address-ஐ பெறுகிறது.

SIZE

Explanation
Core idea

ஒரு object memory-ல் எவ்வளவு space பயன்படுத்துகிறது என்பதே size.

Detail 01

int x = 10;

Program
C++ 1 lines
1cout << sizeof(x);
Concept
  1. x
  2. type = int
  3. int representation requirement
  4. object size

LIFETIME

Concept

ஒரு object memory-ல் எவ்வளவு நேரம் exist செய்கிறது என்பதே அதன் lifetime.

முக்கியமான concept.

  1. Object created
  2. Object exists
  3. Object used
  4. Object destroyed

இந்த entire period:

Object Lifetime

Program
Example
C++ 4 lines
1void test()
2{
3    int x = 10;
4}
Explanation
Core idea

அதனால் x-ன் lifetime function execution duration-க்கு உட்பட்டது.

Flow
  1. test() starts
  2. x created
  3. x exists
  4. test() ends
  5. x destroyed

SCOPE

Explanation
Core idea

Scope என்பது object/name program-ன் எந்த பகுதியில் accessible என்பதைக் குறிப்பிடுகிறது.

Program
Example
C++ 5 lines
1void test()
2{
3    int x = 10;
4    cout << x;
5}
Explanation
Core idea

x function block-க்குள் accessible.

Detail 01

Outside:

Program
C++ 1 lines
1cout << x;
Explanation
Core idea

பொதுவாக அந்த local declaration visible இல்லாததால் access செய்ய முடியாது.

Detail 01

SCOPE vs LIFETIME

Detail 02

மிகவும் முக்கியமான TRB concept.

Detail 03
  • Scope
  • Where can the name be used?
  • Lifetime
  • How long does the object exist?
தமிழில்

Scope = எங்கே access செய்யலாம்?
Lifetime = எவ்வளவு காலம் memory-ல் இருக்கும்?

MUTABILITY

Explanation
Core idea

Object-ன் value change செய்ய முடியுமா என்பதை mutability குறிக்கிறது.

Program
Example
C++ 2 lines
1int x = 10;
2x = 20;
Explanation
Core idea

Value change செய்ய முடிகிறது.

Detail 01

Conceptually mutable.

Detail 02

Constant Object
const int x = 10;

Detail 03

பின்னர்:

Program
C++ 1 lines
1x = 20;
Explanation
Core idea

allowed இல்லை.

இதனால்

Mutable Object
→ value can change

Detail 02

Immutable/constant object
→ value cannot be changed through that object

STORAGE DURATION

Explanation
Core idea

Object memory-ல் எந்த விதத்தில், எவ்வளவு காலம் storage பெறுகிறது என்பதுடன் தொடர்புடைய concept.

C++-ல் முக்கிய storage durations
  • Automatic
  • Static
  • Thread
  • Dynamic
Intro level-ல் முக்கியமானவை
  • Automatic Storage
  • Static Storage
  • Dynamic Storage
  • Automatic Object
Program
Example
C++ 4 lines
1void fun()
2{
3    int x = 10;
4}
Explanation
Core idea

x block enter செய்யும்போது created.

Detail 01

Block exit செய்யும்போது destroyed.

Program
C++ 15 lines
1Enter block
2
3x created
4
5use x
6
7exit block
8
9x destroyed
10Static Object
11void fun()
12{
13    static int count = 0;
14    count++;
15}
Explanation
Core idea

count function call முடிந்த பிறகும் அதன் value preserve ஆகும்.

Detail 01
fun();
fun();
fun();
Conceptually
  • Call 1 → 1
  • Call 2 → 2
  • Call 3 → 3
Detail 03

Object storage program execution முழுவதும் நீடிக்கக்கூடும்.

Detail 04

Dynamic Object

Detail 05

Program execution time-ல் explicitly allocate செய்யப்படும் object.

C++ example

int *p = new int(10);

Concept
  1. new
  2. memory allocated
  3. integer object created
  4. p points to object

Later:

delete p;

Memory release செய்ய வேண்டும்.

BINDING

Concept

ஒரு object-ன் property ஒரு entity-க்கு associate செய்யப்படுவதை binding என்கிறோம்.

Explanation
Example
Core idea

int x = 10;

இதில்

x → int

Detail 02

என்பது name-to-type binding.

Detail 03

x → memory location

Detail 04

என்பது name-to-location binding.

Detail 05

x → 10

Detail 06

என்பது current value association.

Detail 07

TYPE AND OBJECT RELATIONSHIP
int number = 50;

இதனை முழுமையாக break செய்தால்
  1. int
  2. Type
Detail 09
  1. number
  2. Object / Variable name
Detail 10
  1. 50
  2. Value
Detail 11
  1. Memory
  2. Storage location
Full relation
  1. Type
  2. defines properties
  3. Object
  4. occupies storage
  5. holds ValueTYPE vs VALUE vs OBJECT
Detail 13

இந்த மூன்றும் confuse ஆகக்கூடாது.

Detail 14

int x = 100;

Table
ConceptAnswer
Typeint
Objectx
Value100
Namex
Memorystorage occupied by x
Explanation
Core idea

VARIABLE vs OBJECT

Intro programming-ல்

int x = 10;

Detail 02

x-ஐ variable என்றும் சொல்லலாம்.

Detail 03

Broader programming language terminology-ல் அது ஒரு object/data object ஆகவும் பார்க்கப்படும்.

Detail 04

Variable

Detail 05

Value change செய்யக்கூடிய named entity என்ற basic idea.

Detail 06

Object

Detail 07

Storage மற்றும் value-ஐ represent செய்யும் broader concept.

Detail 08

TYPE PROPERTY SUMMARY

ஒரு type-ன் முக்கிய properties
  1. TYPE
  2. +-- Set of values
  3. +-- Allowed operations
  4. +-- Representation
  5. +-- Size
  6. +-- Range
  7. +-- Precision
  8. +-- Compatibility
  9. +-- Conversion rules
  10. +-- Type checking
  11. +-- Type equivalenceOBJECT PROPERTY SUMMARYOBJECT
  12. +-- Name
  13. +-- Type
  14. +-- Value
  15. +-- Memory location
  16. +-- Size
  17. +-- Scope
  18. +-- Lifetime
  19. +-- Mutability
  20. +-- Storage duration
Comparison
Type vs Object
TypeObject
ClassificationActual entity
Defines valid valuesHolds a value
Defines operationsOperations are performed on it
Determines representationOccupies/uses storage according to representation
Example: intExample: x
Conceptual descriptionRuntime/data entity
Explanation
Example
Core idea
int x = 10;
int y = 20;
Here

Type:
int

Detail 02
  • Objects:
  • x
  • y
Detail 03

Values:
10
20

Common Mistake
Error 1

int is an object

Incorrect in this context.

int is a type.

Common Mistake
Error 2

10 is the variable

Incorrect.

10 = value

Common Mistake
Error 3

Scope and lifetime are same

Incorrect.

Scope
visibility of name
Lifetime
existence duration of object
Common Mistake
Error 4

Every object means only class object.

Incorrect.

Programming language theory-ல் data object என்ற broader meaning உண்டு.

Remember
  1. TOV

  2. T → Type

  3. O → Object

  4. V → Value

Program
Example
C++ 4 lines
1int age = 25;
2T = int
3O = age
4V = 25
Explanation
Object properties நினைவில் வைக்க
  • NTVASLM
  • N → Name
  • T → Type
  • V → Value
  • A → Address
  • S → Size / Scope
  • L → Lifetime
  • M → Mutability
Important
  1. Type determines the set of values and operations.

  2. Object is an actual program entity that can hold a value.

  3. One type can have many objects.

  4. An object's value may change while its identity/storage entity remains.

  5. Scope and lifetime are different concepts.

  6. Type checking may occur at compile time or runtime.

  7. Type equivalence can be based on name or structure.

  8. Object properties include type, value, storage, scope and lifetime.

TRB Point
  1. TRB/NET/SET exam-க்கு மிகவும் முக்கியமான statements

    Question: A data type defines what?

  2. Answer

    A set of values together with the operations permitted on those values.

  3. Question

    Scope of a variable refers to?

  4. Answer

    The region of program in which the name is visible/accesssible.

  5. Question

    Lifetime of an object refers to?

  6. Answer

    The period during program execution for which the object exists.

  7. Question

    Type checking performed before execution is?

  8. Answer

    Static type checking.

  9. Question

    Type checking during execution is?

  10. Answer

    Dynamic type checking.

Interview Point

Consider:

int x = 10;

Interview Point
Question
Context

"What are the type, object and value?"

Type   = int
Object = x
Value  = 10

x = 50;

Object = x remains the same
Value = changes from 10 to 50
MCQ
119A data type primarily defines:
Choose one answer
MCQ
120In the declaration: int x = 10; int represents:
Choose one answer
MCQ
121In: int x = 10; 10 represents:
Choose one answer
MCQ
122The region in which a name can be accessed is called:
Choose one answer
MCQ
123The period for which an object exists is called:
Choose one answer
MCQ
124Compile-time type checking is called:
Choose one answer
MCQ
125An object's current stored information is its:
Choose one answer
MCQ
126Type equivalence based on internal structure is:
Choose one answer
Practice

For the following statement identify Type, Object and Value:

double salary = 45000.50;

Answer:

Type   = double
Object = salary
Value  = 45000.50

Now:

char grade = 'A';

Answer:

Type   = char
Object = grade
Value  = 'A'
Summary
  1. TYPE
  2. defines
  3. Values + Operations +Representation
  4. OBJECT
  5. has
  6. Name + Type + Value + Address
  7. alsohas
  8. Scope + Lifetime + Storage +Mutability
  9. ஒரே line-ல் நினைவில் வைக்க
  10. Type என்பது data-க்கு rule book;Object என்பது அந்த rules-ஐ பின்பற்றி program memory-ல் இருக்கும் actual entity.
  11. Next syllabus connection
  12. Properties of Types and Objects →Scalar Data Types → Integer, Floating Point, Character, Boolean, Enumeration → Composite Data Types.
Text size
17px
Theme
Contents
Print / PDF
Program