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

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

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

  1. #include
    #include
    using namespace std;
    class Point {
    public:
    void setArray(int** arr, int Row, int Col) {

    for (int i = 0; i < Row; i ) {
    arr[i] = new int[Col];
    for (int g = 0; g < Col; g++) {
    arr[i][g] = rand() % 50;
    }
    }
    }
    void swap(int **arr,int **arr1,int Col,int Row) {

    for (int g = 0; g < Col; g++) {
    arr1[g] = new int[Row];
    for (int i = 0; i < Row; i ) {
    arr1[g][i] = arr[i][g];
    }
    }
    }
    };
    void main() {
    int Row = 4, Col = 2;
    srand(time(NULL));
    Point a;
    int** arr;
    int** arr1;
    arr1 = new int* [Col];
    arr = new int* [Row];
    a.setArray(arr, Row, Col);
    for (int i = 0; i < Row; i ) {
    for (int g = 0; g < Col; g++) {
    cout << arr[i][g] << "\t";
    }
    cout << endl;
    }
    a.swap(arr, arr1, Col, Row);
    cout << endl << endl;
    for (int g = 0; g < Col; g++) {
    for (int i= 0; i < Row; i ) {
    cout << arr1[g][i] << "\t";
    }
    cout << endl;
    }
    }

  2. Pflfxf #2

    #include
    #include

    void sp() {
    std::cout << '\n';
    }

    class Children {
    private:
    std::string firstName;
    std::string lastName;
    int age{};
    public:
    void enterInformation() {
    std::cout <> firstName;

    std::cout <> lastName;

    std::cout <> age;

    sp();
    }
    void printInformation() {
    std::cout << "Name: " << firstName;
    sp();
    std::cout << "Last name: " << lastName;
    sp();
    std::cout << "Age: " << age;
    }
    };

    int main()
    {
    Children a;
    a.enterInformation();
    a.printInformation();

    return 0;
    }

  3. Complete the class, including the verification method
    on the parity of integer values ​​of tree nodes.
    Odd values ​​must be increased to
    the nearest pair, and duplicates
    should not be created
    ”Please help me very much”

  4. Complete the class, including the verification method
    on the parity of integer values ​​of tree nodes.
    Odd values ​​must be increased to
    the nearest pair, and duplicates
    should not be created

  5. #include
    using namespace std;

    class Matrica
    {
    public:
    void vvod()
    {
    cin >> ctroka;
    cin >> stolbcy;
    int** mass = new int* [ctroka];
    int** mass2 = new int* [stolbcy];
    Mass(mass, mass2);
    zamena(mass, mass2);
    vyvod(mass, mass2);
    }

    private:
    int ctroka, stolbcy;

    void Mass(int** mass, int** mass2)
    {
    for (int i = 0; i < stolbcy; i )
    mass[i] = new int[stolbcy];

    for (int i = 0; i < stolbcy; i )
    for (int j = 0; j > mass[i][j];

    system(“cls”);

    for (int i = 0; i < stolbcy; i )
    {
    for (int j = 0; j < ctroka; j )
    cout << mass[i][j] << " ";
    cout << endl;
    }

    for (int i = 0; i < ctroka; i )
    mass2[i] = new int[ctroka];
    }

    void zamena(int** mass, int** mass2)
    {
    for (int i = 0; i < ctroka; i )
    for (int j = 0; j < stolbcy; j )
    mass2[i][j] = mass[j][i];
    }

    void vyvod(int** mass, int** mass2)
    {
    for (int i = 0; i < ctroka; i )
    {
    for (int j = 0; j < stolbcy; j )
    cout << mass2[i][j];
    cout << " " << endl;
    }
    }
    };

    void main()
    {
    Matrix A;
    A.vvod();
    }

  6. Create a class, which includes the results of testing students in a group in three subjects,and then get a function to input and output data.
    Include the members of the following function.
    1. The surname of the student is produced,who did not pass at least one class
    2. Academic quality,that is, the percentage of students,who scored more 75 points.
    3. Name of the course with the highest percentage of achievement
    4. List in descending order of student achievement

  7. #include <iostream> using namespace std; class Matrix { private: int** arr; int** arr2; int rows, cols; public: void Fill_array(int rows_value, int cols_value) { rows = rows_value; cols = cols_value; arr = new int* [rows]; for (int i = 0; i < rows; i ) { arr[i] = new int[cols]; } for (int i = 0; i < rows; i ) { for (int j = 0; j < cols; j ) { arr[i][j] = 10 + rand()% + 50; cout << arr[i][j]<< "\t"; } cout << endl; } } void Change_rows_with_cols() { arr2 = new int* [cols]; for (int i = 0; i < cols; i ) { arr2[i] = new int[rows]; } cout << endl << endl; for (int i = 0; i < cols; i ) { for (int j = 0; j < rows; j ) { arr2[i][j] = arr[j][i]; cout << arr2[i][j] << "\t"; } cout << endl; } } ~ Matrix() { for (int i = 0; i < cols; i ) { delete[]arr2[i]; } cout << "Destructor for second matrix" << endl; delete[]arr2; for (int i = 0; i < rows; i ) { delete[]arr[i]; } cout << "Destructor for first matrix" << endl; delete[]arr; } }; int main() { Matrix first; first.Fill_array(2, 4); first.Change_rows_with_cols(); }

Leave a Reply

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