The basics of programming in c++ for beginners

Tasks and Solutions: Classes C ++

Прошло немного времени после публикации вводного урока о классах в C . Теперь мы предлагаем вам попрактиковаться в решении достаточно простых задач с применением классов. Старайтесь решать самостоятельно.

1. Create classTiles (кафель), который будет содержать поля с открытым доступом: brandsize_hsize_wprice class and methodgetData(). В главной функции объявить пару объектов класса и внести данные в поля. Затем отобразить их, calling methodgetData().

2. Determine the class Children, which contains the following fields(class members): закрытые – имя ребенка, фамилию и возраст , публичные – методы ввода данных и отображения их на экран. Объявить два объекта класса, внести данные и показать их.

3. Преобразовать строки двумерного массива в столбцы. Например было: 

tasks and solutions, c ++ classes, c++ classes for beginners, privat and public access specifiers, class methods

стало:

tasks and solutions, c ++ classes, c++ classes for beginners, privat and public access specifiers, class methods

Класс будет содержать два метода: один заполнит массив значениями, второй произведет замену значений строк на значения столбцов.

17 thoughts on “Tasks and Solutions: Classes C ++

  1. That's how I made the third task:
    #include “pch.h”
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;
    class Matrix
    {
    int rows, cols;
    int arr[100][100];
    int i, j;
    public:
    void Fill_array()
    {
    this->cols = cols;
    this->rows = rows;
    cout <> rows; cout << endl;
    cout <> cols; cout <arr[rows][cols];

    for ( i = 0; i < rows; i )
    {
    for ( j = 0; j < cols; j )
    {
    arr[i][j] = rand() % 10;
    }
    }
    }
    void Change_array()
    {
    cout << "Matrix\n\n";
    for ( i = 0; i < rows; i )
    {
    for ( j = 0; j < cols; j )
    {
    cout << setw(4) << arr[i][j];
    }
    cout << endl;
    }
    cout << "\nModified matrix\n\n";
    for ( j = 0; j < cols; j )
    {
    for ( i = 0; i < rows; i )
    {
    cout << setw(4) << arr[i][j];
    }
    cout << endl;
    }
    }
    };

    int main()
    {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    srand(time(NULL));

    Matrix a;
    a.Fill_array();
    a.Change_array();

    return 0;
    }

  2. //1 the task.
    class Tiles
    #include
    {public:
    string brand;
    int size_h;
    int size_w;
    double price; int data;
    void setData(int x,int y)
    {
    size_h=x;
    size_w=y;
    }
    void getData()
    {
    cout << "\tНазвание = " << brand << "\tВысота= " << size_h << "\tШирина = " << size_w << "\tСтоимость = " << price << endl;

    }
    void Print()
    {
    cout <<"\tНазвание = "<< brand << "\tВысота= " << size_h << "\tШирина = " << size_w << "\tСтоимость = " << price << endl;
    }

    };
    int main()
    {
    setlocale(LC_ALL, "rus");
    Tiles Keramika;
    Keramika.brand = "Керамика из Италии";
    Keramika.price = 2.3;
    Tiles bKeramika;
    Keramika.setData(5, 6);
    Keramika.getData();
    bKeramika.price=30.5;
    bKeramika.size_h=20;
    bKeramika.size_w = 10;
    bKeramika.brand = "Керамика из Франции";
    bKeramika.Print();
    }

  3. //void FillArray-class method. void Exchange-separate function
    #include
    using namespace std;
    int **arr;
    class Inverse
    {
    public:
    void FillArray(int raw, int col)
    {
    arr = new int*[raw];
    for (int i = 0; i < raw; i )
    {
    arr[i] = new int[col]; //assigning to each element of a new line array dyn, responsible for speakers
    }

    for (int i = 0; i < raw; i )
    {
    cout << "|";
    for (int j = 0; j < col; j )
    {
    arr[i][j] = rand() % 20;
    cout << arr[i][j] << "\t";
    }
    cout << "|" << endl;
    }
    }
    };

    void Exchange(int raw, int col)
    {
    ** = new NARR you're *[col];

    for (int i = 0; i < col; i )
    {
    fool[i] = new int[raw]; //assigning to each element of a new line array dyn, responsible for speakers
    }
    for (int i = 0; i < col; i )
    {
    for (int j = 0; j < raw; j )
    {
    fool[i][j] = arr[j][i];
    }
    cout << endl;
    }
    for (int i = 0; i < raw; i )
    {
    delete[] arr[i];//rid of old data from the array
    }
    delete[] arr;
    arr = new int*[col];
    for (int i = 0; i < col; i )
    {
    arr[i] = new int[raw];
    }
    for (int i = 0; i < col; i )
    {
    cout << "|";
    for (int j = 0; j < raw; j )
    {
    arr[i][j] = fool[i][j];
    cout << arr[i][j] << "\t";
    }
    cout << "|" << endl;
    }

    for (int i = 0; i < col; i )
    {
    delete[] fool[i];
    }
    delete[] fool;
    }

    int main()
    {
    int raw = 7;
    int col = 10;
    Inverse a;
    a.FillArray(raw, col);
    Exchange(raw, col);
    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *