Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Object Oriented

Programming
Concepts
Lecture: User defined Data types

Superior Collage Jauharabad


Introduction:
A user defined datatype is a type which
user create for its own benefits or use.
 Structure
 Enumerations
 Typedef

SUPERIOR COLLAGE JAUHARABAD


Structure (struct)

SUPERIOR COLLAGE JAUHARABAD


Structure (struct)
There are many instances in programming
where we need more than one variable in
order to represent an object.
For example: to represent yourself, you might
want to store your name, your birthday, your
height, your weight, or any other number of
characteristics about yourself. For Example

SUPERIOR COLLAGE JAUHARABAD


Contind……
string myName;
int myBirthYear;
int myBirthMonth;
int myBirthDay;
int myHeight;
int myWeight;

SUPERIOR COLLAGE JAUHARABAD


Contind……
you now have 6 independent variables that are not
grouped in any way. If you wanted to pass information
about yourself to a function, you’d have to pass each
variable individually.
 Furthermore, if you wanted to store information
about someone else, you’d have to declare 6 more
variables for each additional person!

SUPERIOR COLLAGE JAUHARABAD


Contind……
C++ allows us to create our own user-defined
aggregate data types.
An aggregate data type is a data type that groups
multiple individual variables together.
One of the simplest aggregate data type is the struct.

SUPERIOR COLLAGE JAUHARABAD


Structure (struct)
Structure is the collection of variables of
different types under a single name for
better visualization of problem.
struct (short for structure) allows us to
group variables of mixed data types
together into a single unit.

SUPERIOR COLLAGE JAUHARABAD


Structure (struct)
 Arrays is also collection of data but
arrays can hold data of only one type
whereas structure can hold data of one or
more types

SUPERIOR COLLAGE JAUHARABAD


define a structure in C++
The struct keyword defines a structure type followed
by an identifier(name of the structure). 
struct Employee
{
    string Name;
    int age;
    double salary;
};
This tells the compiler that we are defining a struct
named Employee. The Employee struct contains 3
variables inside of it

SUPERIOR COLLAGE JAUHARABAD


define a structure in C++
When a structure is created, no
memory is allocated.
The structure definition is only the
blueprint for the creating of variables
You can imagine it as a datatype

SUPERIOR COLLAGE JAUHARABAD


How to define a structure
variable?
Once you declare a structure you can
define a structure variable as:
Employee emp;
This defines a variable of type Employee named
joe. As with normal variables, defining a struct
variable allocates memory for that variable.

SUPERIOR COLLAGE JAUHARABAD


How to access members of a
structure?
The members of structure variable is accessed using
dot (.) operator.
To access members of structure variable emp and
assign values to it. You can perform this task by using
following code below:
Employee emp; // create an Employee struct emp
emp.name = “Ali”; // assign a value to member name
emp.age = 32; // assign a value to member age
emp.salary= 25000; // assign a value to member salary

SUPERIOR COLLAGE JAUHARABAD


Initializing structs
Initializing structs by assigning values member by member is
a little awkward
C++ supports a faster way to initialize structs using
an initializer list
struct Employee
{
    string name;
    int age;
    double wage;
};
 Employee emp= { “Ali Imran”, 32, 25000};
// emp.name = “Ali iman”, emp.age = 32, emp.wage = 60000

SUPERIOR COLLAGE JAUHARABAD


Complete Example:

SUPERIOR COLLAGE JAUHARABAD


Quiz
You are running a website, and you are trying to keep
track of how much money you make per day from
advertising. Declare an advertising struct that keeps
track of how many ads you’ve shown to readers, what
percentage of users clicked on ads, and how much you
earned on average from each ad that was clicked.
Read in values for each of these fields from the user.
Pass the advertising struct to a function that prints
each of the values, and then calculates how much you
made for that day (multiply all 3 fields together).

SUPERIOR COLLAGE JAUHARABAD


Enumeration (Enum)

SUPERIOR COLLAGE JAUHARABAD


introduction
An enumeration is a user-defined type
whose value is restricted to one of several
explicitly named constants(enumerators).
An enumerated type (also called
an enumeration) is a data type where every
possible value is defined as a symbolic
constant (called an enumerator).

SUPERIOR COLLAGE JAUHARABAD


Defining an Enum
Enumeration are defined using keyword: enum.

enum seasons
{ spring, summer, autumn, winter };

enum days
{ Mon, Tue, Wed, Thu, Fri, Sat, Sun};

each enumerator is separated by a comma, and the entire enumeration


is ended with a semicolon.

SUPERIOR COLLAGE JAUHARABAD


Defining an Enum
After defining enum we can treat it like a datatype
#include <iostream>

using namespace std;

enum Gender
{Male, Female};

Void main()
{
Gender g;
g = Male; // Correct
g = He; // Error
}

SUPERIOR COLLAGE JAUHARABAD


Defining an Enum
Declaring an enumeration does not
allocate any memory.
when a variable of the enumerated
type is defined, memory is allocated
for that variable at that time.

SUPERIOR COLLAGE JAUHARABAD


Enumerator values
Each enumerator is     WEDNESDAY, // assigned 2
automatically assigned an     THURSDAY, // assigned 3
integer value based on its     FRIDAY, // assigned 4
position in the     SATURDAY, // assigned 5
enumeration list. 
    SUNDAY, // assigned 6
By default, the value of    };
first enumerator is 0,
second is 1 and so on
One very important thing
to remember is
that, spring, summer etc
are not variables. They are
treated as integers by
compiler.
SUPERIOR COLLAGE JAUHARABAD
Enumerator values
It is possible to explicitly define the value of enumerator. 
These integer values can be positive or negative and can share the
same value as other enumerators.
enum Animal
{
    CAT = -3,
    DOG, // assigned -2
    PIG, // assigned -1
    HORSE = 5,
    GIRAFFE = 5, // shares same value as ANIMAL_HORSE
    CHICKEN // assigned 6
};

SUPERIOR COLLAGE JAUHARABAD


Enumerator values
#include <iostream>
using namespace std;
enum seasons
{ spring, summer, autumn, winter };

void main()
{
seasons s;
s = spring;
cout >> "spring = " >> s >> endl;
s = summer;
cout >> "summer = " >> s >> endl;
s = autumn;
cout >> "autumn = " >> s >> endl;
s = winter;
cout >> "winter = " >> s >> endl;
}

SUPERIOR COLLAGE JAUHARABAD


Enumerator values
Output:

spring = 0
summer = 1
autumn = 2
winter = 3

SUPERIOR COLLAGE JAUHARABAD


Typedef

SUPERIOR COLLAGE JAUHARABAD


Introduction
A type alias is a different name by which a type
can be identified.
In C++, any valid type can be aliased so that it
can be referred to with a different identifier.
Typedefs allow the programmer to create an
alias for a data type, and use the aliased name
instead of the actual type name.
To declare a typedef, simply use
the typedef keyword, followed by the type to
alias, followed by the alias name
SUPERIOR COLLAGE JAUHARABAD
Defining typedef
Synten:

typedef existingDatatype newAlias;


Example:
typedef int age;

SUPERIOR COLLAGE JAUHARABAD


Benefits
Typedefs also allow you to change the
underlying type of an object without having to
change lots of code.
For example, if you were using a short to hold a
student’s ID number, but then later decided you
needed a long instead, you’d have to comb through
lots of code and replace short with long.
However, with a typedef, all you have to do is
change 
typedef short studentID  To typedef long studentID.
SUPERIOR COLLAGE JAUHARABAD
Example
#include <iostream>
#include <string>
using namespace std;
Typedef int Age;
Typedef string Name;

void main()
{
Age var1;
Name var2;
cout >> “Enter Name";
cin << var2;
cout >> “Enter Age ";
cin << var1;
cout >> “Name is " >> var2 >> endl;
cout >> “Age is " >> var1 >> endl;
}

SUPERIOR COLLAGE JAUHARABAD


That’s all of today..!!

SUPERIOR COLLAGE JAUHARABAD

You might also like