C ා object oriented programming class concept and design details

Keywords: Programming Attribute C

1, The basis of object-oriented programming

1.1 object oriented programming

(1) What is object-oriented programming

In C language, the designed application program is too single, because all the functions are contained in several or even one code block, which can only serve a single program. Therefore, in order to increase the reuse opportunities of these code blocks and complete more programs, the object-oriented programming method should be used.

The object-oriented programming can make every computer application program composed of a single object (or unit) which can play the role of word program.

(2) Concepts of classes and objects

Object-oriented programming is an object-based, event driven programming technology. Object is the basic element of a program. Events and their handlers establish the relationship between objects.

A, class

There are countless entities in life, such as people, cars, plants, etc. each entity has a series of properties and behaviors. For example, a car can define its color, model and other properties, as well as its forward, backward and other behaviors. In object-oriented design, a specific definition of the nature and behavior of an entity is called a class.

B, object

Object is the core of object programming technology and the basic element of application program. In a class, each object is an instance of that class. For example, human is a class in the entity of human, and each different person is the object of human. Each person has different height, weight and other properties as well as the behavior of standing up and walking. In object-oriented programming, the nature of an object is called an attribute, the events occurring on the object are called events, and the behaviors generated for an event are called methods.

Attributes, methods and events constitute the three elements of an object.

(3) , predefined classes

The controls, user-defined forms, and data types that you just started learning are provided by the. NET Framework and are called predefined classes.

1.2 encapsulation and concealment

Encapsulation refers to organizing members into a class. In C, the members of a class include data members, properties, methods, and events. Class is a tool to implement encapsulation, which guarantees the independence of class.

(1) Defining classes

In C, a class is defined by class. For example, Form1 is a class keyword defined class.

The general format of the custom class is as follows:

Class class name
{
    //Define data members
    //Defining properties
    //Defining methods
    //Define events
}

(2) Define class members

In the definition of a class, it also provides the definition of all members of the class, including data, properties, events, and methods. It can be defined by the following modifiers:

Modifier Significance
public Class members can be accessed by any code
private Class members can only be accessed by code in the class. When defining members, private is used by default
protected Class members can only be accessed by code in a class or its derived classes (that is, subclasses)

A. Define data members

The data members of the class are defined by standard variable declaration statements, and access modifiers are used to specify the access level of the data members. For protection, data members are generally declared with private and protected modifiers.

    class vehicle
    {
        private int wheels;     //Number of wheels
        private float weight;   //Vehicle weight
    }

B. Defining methods

Class is defined by a standard function declaration statement, and the access level of the method is specified by the access modifier.

    public class Vehicle
    {
        private int wheels;
        private float weights;
        public void SetVehicle(int wheels,float weights)  //Define method SetVehicle to set the number of wheels and vehicle weight
        {
            this.wheels = wheels;
            this.weights = weights;
        }
        public void GetVehicle()     //Define the method GetVehicle() to get the number of wheels and vehicle weight
        {
            MessageBox.Show("Number of wheels:" + this.wheels.ToString() + "\n Vehicle weight:" + this.weights.ToString());
        }
    }
        private void button1_Click(object sender, EventArgs e)
        {
            int wheels;
            float weight;
            wheels = int.Parse(textBox1.Text);
            weight = float.Parse(textBox2.Text);
            Vehicle v = new Vehicle();
            v.SetVehicle(wheels, weight);
            v.GetVehicle();
        }

In the SetVehicle() method, there are two sets of wheels and weight variables. A group assigns values to the two data members wheels and weight of the Vehicle, while GetVehicle

(3) Defining properties

The properties of a class can be defined in C ා: one is used to set the property value, which is defined by the keyword set; the other is used to get the property value, which is defined by the get keyword. One of the code blocks can be ignored to set read-only or write only attributes.

Access modifier data type property name
{
    set
    {
         ... / / set code block of attribute
    }
    get
    {
         ... / / get code block of attribute
    }
}
namespace WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            float x, y;
            string msg1, msg2;
            x = float.Parse(textBox1.Text);
            y = float.Parse(textBox2.Text);
            Point p = new Point();
            p.MyX = x;
            p.MyY = y;
            msg1 = "The coordinates you entered are(" + p.MyX.ToString() + "," + p.MyY.ToString() + ")";
            label3.Text = msg1;
            msg2 = p.ReadXY;
            MessageBox.Show("The coordinates you entered are" + msg2 + "!");
        }
    }
    class Point
    {
        private float x;    //x coordinates
        private float y;    //y coordinates

        public float MyX     //Define property MyX
        {
            set
            {
                x = value;   //Provide assignment to data member x
            }
            get
            {
                return x;     //Provide access to x
            }
        }
        public float MyY
        {
            set
            {
                y = value;
            }
            get
            {
                return y;
            }
        }
        public string ReadXY
        {
            get
            {
                return "(" + x + "," + y + ")";
            }
        }
    }
}

Properties actually provide a way to access private data members in a class. The definition of attribute not only solves the problem of access to data members, but also provides powerful operation control.

1.3 access to objects and their members

In the object-oriented programming, we must abide by the principle of "define first, use later", that is, any predefined or customized class must instantiate the scale object before it can be used.

(1) Object declaration

Type object name = new class name ();

(2) Access to members

The members defined in a class usually need to be accessed through objects. For different types of data members, their access forms are also different.

Data member: object. Data member

Property: object. Property

Method: object. Method

Event: complex, please refer to MSDN official help document

Example: to create a student class, you can read in the data through the read() method and output the data through the write method.

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            string id, name;
            float score;

            Console.Write("Please enter student ID:");
            id = Console.ReadLine();
            Console.Write("Please enter your name:");
            name = Console.ReadLine();
            Console.Write("Please enter the score:");
            score = float.Parse(Console.ReadLine());

            student s = new student();
            s.SetInfo(id, name);
            s.MyScore = score;

            Console.WriteLine("\n======Here is the output=======\n");
            Console.WriteLine(s.OutPut());
            Console.WriteLine("\n Press any key to end the program:");
            Console.ReadKey();
        }
    }
    class student
    {
        private string sId; //Student ID
        private string sName; //Full name
        private float score;  //Fraction

        public void SetInfo(string sId,string sName)  //Define method SetInfo() to set student number and name
        {
            this.sId = sId;
            this.sName = sName;
        }

        public float MyScore    //Define property MyScore
        {
            set
            {
                score = value;
            }
            get
            {
                return score;
            }
        }
        public string OutPut()    //Define the method OutPut to provide the formatted OutPut of all data members
        {
            return "Student ID:" + sId + " Full name:" + sName + " Score:" + score;
        }
    }
}

1.4 constructors and destructors

(1) Constructor

Constructors can assign values to data members while declaring objects. Use with the new keyword when declaring objects.

 

95 original articles published, praised 331, visited 500000+
Private letter follow

Posted by grglaz on Sat, 14 Mar 2020 21:09:55 -0700