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

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

37 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;
    }

  4. Without pointers, and one method. Who gives a warning, add key -std = c ++ 11 compiler. But work without it.

    #include
    using namespace std;
    class mass
    {
    int massStroka[5][2]={};
    int massCtolbez[2][5]={};
    public:
    void zapolnMass()
    {
    int a =0;
    int kolvoStr;
    int kolvoCtlb;
    cout<<" Enter the number of rows. "<>kolvoStr;
    cout<<" Enter the number of columns. "<>kolvoCtlb;
    for (int i=0;i<kolvoStr;i )
    {
    cout<<endl;
    for(int j =0;j<kolvoCtlb;j )
    {
    a++;

    massStroka[i][j] = a;
    massCtolbez[j][i] = a ;
    cout << "|" ;
    cout<<massStroka[i][j];
    cout << "|" ;
    }
    }
    cout<<endl;

    for (int z=0;from<kolvoCtlb;with ++)
    {
    cout<<endl;
    cout << "|";
    for(int k =0;k<kolvoStr;k )
    {
    cout<<massCtolbez[from][k];
    }
    cout << "|";
    }
    }
    };

    int main ()
    {
    setlocale(LC_ALL, "rus");
    mass a1;
    a1.zapolnMass();

    return 0;
    }

  5. 3 the task. Prompt, all is good?

    #include
    #include

    using namespace std;

    class Matrix
    {
    private:
    int arr[3][4];
    public:
    void vivodnorn()
    {
    for (int i = 0; i < 3; i )
    {
    for (int k = 0; k < 4; k )
    {
    arr[i][k] = rand() % 132;
    cout << arr[i][k] << "\t";

    }
    cout << endl;
    }
    }

    void vivodfuc()
    {
    for (int i = 0; i < 4; i )
    {
    for (int k = 0; k < 3; k )
    cout << arr[k][i] << "\t";
    cout << endl;
    }
    }
    };

    int main(int vage)
    {
    setlocale(0, "");
    srand(time(0));

    Matrix a;
    a.vivodnorn();
    cout << endl << endl;
    a.vivodfuc();

    return 0;
    }

  6. Through the class constructor 1 the task is easier to do
    #include
    #include
    using namespace std;
    class Tiles {
    private:
    double size_h;
    double size_w;
    double price;
    string Brend;
    public:
    Tiles(double size_h1, double size_w1, double price1, string Brend1) {
    size_h = size_h1;
    size_w = size_w1;
    price = price1;
    Brand = Brand1;
    }
    void Getobject() {
    cout << size_h << " X " << size_w << endl << "$" << price << endl << "Bramnd " << Brand << endl;
    }
    };
    void main() {
    Tiles a(12,14.5,20.5,"EPTA");
    a.Getobject();
    }

  7. Second number
    #include
    #include
    #include
    using namespace std;
    class Children {
    private:
    int age;
    string name;
    string surname;
    public:
    void SetChildren() {
    cin >> age;
    cin >> name;
    cin >> surname;
    }
    void Print() {
    cout << "имя "<<name<<endl<<"Фамилия "<<surname<<endl<<"Возраст"<<age<<endl;
    }
    };
    void main() {
    SetConsoleCP(1251);
    SetConsoleOutputCP(1251);
    Children a;
    a.SetChildren();
    a.Print();
    }

Leave a Reply

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