for Robot Artificial Inteligence

15.Constructors example

|

CAR

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct car
{
    char model[20];
    int year;
    int ID;
}mycar, cars[1000]; // cars[0], cars[1]
int main()
{
    strcpy(mycar.model, "Fast and furious");
    mycar.year = 2017;

    cout<<mycar.model[0]<<mycar.model[1]<<mycar.model[2];
    cout<<mycar.model[3];

    return 0;
}

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct car
{
    char model[20];
    int year;
    int ID;
}mycar, cars[1000]; // cars[0], cars[1]

void printingcar(car car_)
{
    cout<<car_.model<<"\n";
    cout<<car_.ID<<"\n";
}
int main()
{
    strcpy(mycar.model, "Fast and furious");
    mycar.year = 2017;
    cout<<mycar.model[0]<<mycar.model[1]<<mycar.model[2];
    cout<<mycar.model[3]<<"\n";
    for (int i=1; i<=100; ++i)
    {
        cars[i].ID = i;
        strcpy(cars[i].model, "Fast and Furious");
        cars[i].year = 2017;
    }
    printingcar(cars[13]);
    printingcar(cars[37]);
    return 0;

}

Student

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct car
{
    char model[20];
    int year;
    int ID;
}mycar, cars[1000]; // cars[0], cars[1]

void printingcar(car car_)
{
    cout<<car_.model<<"\n";
    cout<<car_.ID<<"\n";
}
int main()
{
    strcpy(mycar.model, "Fast and furious");
    mycar.year = 2017;
    cout<<mycar.model[0]<<mycar.model[1]<<mycar.model[2];
    cout<<mycar.model[3]<<"\n";
    for (int i=1; i<=100; ++i)
    {
        cars[i].ID = i;
        strcpy(cars[i].model, "Fast and Furious");
        cars[i].year = 2017;
    }
    printingcar(cars[13]);
    printingcar(cars[37]);
    return 0;

}

Product

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

struct Product
{
    double price;
    char name[20];
    // initialized
    Product()
    {
        price = 0;
        memset(name,0,sizeof(name));
        // if we don't make the name NULL, it will be randomly intialized.
    }
    Product(double newPrice)
    {
        price = newPrice;
    }

    Product(int Newprice)
    {
        price = Newprice;
    }
    Product(char newName[], double newPrice)
    {
        price = newPrice;
        memset(name,0,sizeof(name));
        strcpy(name, newName);
    }
}student1;// Global variable are initialized: number - 0, char - NULL(nothing)


int main()
{
    Product X("apple", 2.5);
    Product Y(3.6);
    return 0;
}

Comments