How are data and functions organized in an object oriented program? Explain user defined data types in C++.


Q.) How are data and functions organized in an object oriented program? Explain user defined data types in C++.

Subject: Object Oriented Programming and Methodology

How are Data and Functions Organized in an Object-Oriented Program?

In an object-oriented programming (OOP) language like C++, data and functions are organized into objects. An object is a collection of related data and functions that operate on that data. Objects are created from classes, which are blueprints that define the structure and behavior of objects.

Classes

A class is a blueprint that defines the structure and behavior of objects. It contains data members (variables) and member functions (methods) that operate on those data members. Classes are declared using the class keyword, followed by the class name and a pair of curly braces {}.

For example, the following code defines a class called Person:

class Person {
public:
  string name;
  int age;

  void introduce() {
    cout << "My name is " << name << " and my age is " << age << endl;
  }
};

The Person class contains two data members: name and age. It also contains a member function called introduce(), which prints the person's name and age.

Objects

Objects are created from classes using the new keyword. The new keyword allocates memory for the object and calls the class's constructor, which initializes the object's data members.

For example, the following code creates a Person object called p:

Person *p = new Person();

The p object can now access the data members and member functions of the Person class. For example, the following code calls the introduce() method on the p object:

p->introduce();

This will print the person's name and age.

User-Defined Data Types in C++

User-defined data types (UDTs) are data types that are defined by the programmer. They can be used to represent complex data structures that are not built into the C++ language.

UDTs are created using the struct or class keywords. The struct keyword creates a value type, while the class keyword creates a reference type. Value types are stored on the stack, while reference types are stored on the heap.

The following code defines a UDT called Point:

struct Point {
  int x;
  int y;
};

The Point struct contains two data members: x and y. It can be used to represent a point in two-dimensional space.

The following code creates a Point object called p:

Point p;

The p object can now be used to store and manipulate points in two-dimensional space. For example, the following code sets the x and y coordinates of the p object:

p.x = 10;
p.y = 20;

The following code prints the x and y coordinates of the p object:

cout << "The x coordinate of the point is " << p.x << endl;
cout << "The y coordinate of the point is " << p.y << endl;

This will print the following output:

The x coordinate of the point is 10
The y coordinate of the point is 20

UDTs can be used to represent a wide variety of complex data structures, such as lists, queues, stacks, and trees. They are an essential part of OOP and are used extensively in C++.