Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаб. работа 1 с C# и Java.doc
Скачиваний:
4
Добавлен:
17.08.2019
Размер:
94.21 Кб
Скачать

Для самостоятельной работы

Часть 3. Классы и наследование в языке С++/CLI

Каркас консольного С++.NET приложения

/////////////////////////

// ccLab1k2_1

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

int _tmain()

{

// TODO: Please replace the sample code below with your own.

Console::WriteLine(S"Hello World");

return 0;

}

Структура Book

/////////////////////////

// C++.NET File ccLab1k2_1

// Структура Book

#include "stdafx.h"

//#using <mscorlib.dll>

//#include <stdio.h>

//#include <string.h>

//using namespace System;

struct Book

{

int pages;

char name [20];

};

int _tmain ( )

{

// TODO: Please replace the sample code below with your own.

//Console::WriteLine (S"Hello World");

printf ("Hello!\n");

Book book;

book.pages= 500;

strcpy (book.name, "BOOK");

printf ("book %s contains %d pages\n", book.name, book.pages);

return 0;

}

Класс Book

/////////////////////////

// C++.NET File ccLab1k2_2

// Класс Book

#include "stdafx.h"

class Book

{

public:

int pages;

char name [20];

};

int _tmain ( )

{

printf ("Hello!\n");

Book book;

book.pages= 500;

strcpy (book.name, "BOOK");

printf ("book %s contains %d pages\n", book.name, book.pages);

return 0;

}

=========================================

/////////////////////////

// C++.NET File ccLab1k2_2

// Класс Book

#include "stdafx.h"

class Book

{

int pages;

char name [20];

public:

void SetPages (int Pages) {pages= Pages;}

int GetPages ( ) {return pages;}

void SetName (char* pName) {strcpy (name, pName);}

char* GetName ( ) {return name;}

};

int _tmain ( )

{

printf ("Hello!\n");

Book book;

//book.pages= 500;

book.SetPages (300);

//strcpy (book.name, "BOOK");

book.SetName ("BOOK");

printf ("book %s contains %d pages\n", book.GetName ( ),

book.GetPages ( ));

return 0;

}

Наследование

/////////////////////////

// C++.NET File ccLab1k2_4

// Класс Book

#include "stdafx.h"

class Book

{

int pages;

char name [20];

public:

void SetPages (int Pages) {pages= Pages;}

int GetPages ( ) {return pages;}

void SetName (char* pName) {strcpy (name, pName);}

char* GetName ( ) {return name;}

};

class BookOther: public Book

{

bool manual;

public:

void SetMenual (bool Manual) {manual= Manual;}

bool IsManual ( ) {return manual;}

};

int _tmain ( )

{

printf ("Hello!\n");

BookOther book;

book.SetPages (300);

book.SetName ("BOOK");

book.SetMenual (true);

printf ("book %s contains %d pages\n", book.GetName ( ),

book.GetPages ( ));

if (book.IsManual()) printf ("book is manual\n");

else printf ("book isn't manual\n");

return 0;

}

Задание

  1. Самостоятельно создать класс калькулятора Calc с интерфейсом:

void Init (int n) – инициализировать скрытый аккумулятор accumulator

числом n,

int Get ( ) – получить значение accumulator,

void Add (int n) – прибавить n к значению accumulator,

void Sub (int n) – вычесть n из значения accumulator.

  1. Создать класс OtherCalc, порождённый из класса и расширяющий его интерфейс следующей функцией

void Mul (int n) – умножить значение accumulator на указанное число n.

  1. Создать класс AnotherCalc, порождённый из класса OtherCalc и расширяющий его интерфейс следующей функцией

void ChangeSigh ( ) – изменить знак значения accumulator,

bool IsSigh ( ) – получить знак значения accumulator.