C++/Class/Struct Class
Содержание
add method to struct
#include <iostream>
#include <string.h>
using namespace std;
struct Msg
{
char message[256];
void show_message(void);
};
struct UpperMsg
{
char message[256];
void show_message(void);
};
void Msg::show_message(void)
{
cout << message;
}
void UpperMsg::show_message(void)
{
cout << strupr(message);
}
int main(void)
{
Msg book = { "C\n" };
UpperMsg book_upr = { "P\n" };
book.show_message();
book_upr.show_message();
}
Classes and Structures are Related
#include <iostream>
using namespace std;
struct MyClass {
int get_i(void); // these are public by default
void put_i(int j);
private:
int i;
} ;
int MyClass::get_i(void)
{
return i;
}
void MyClass::put_i(int j)
{
i = j;
}
main(void)
{
MyClass s;
s.put_i(10);
cout << s.get_i();
return 0;
}
Constructor and destructor inside a struct
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
struct StringClass {
StringClass(char *ptr);
~StringClass();
void show();
private:
char *p;
int len;
};
StringClass::StringClass(char *ptr)
{
len = strlen(ptr);
p = (char *) malloc(len+1);
if(!p) {
cout << "Allocation error\n";
exit(1);
}
strcpy(p, ptr);
}
StringClass::~StringClass()
{
cout << "Freeing p\n";
free(p);
}
void StringClass::show()
{
cout << p << " - length: " << len;
cout << endl;
}
int main()
{
StringClass stringObject1("www.java2s.com."), stringObject2("www.java2s.com.");
stringObject1.show();
stringObject2.show();
return 0;
}
Stack class using a structure.
#include <iostream>
using namespace std;
#define SIZE 10
struct stack {
stack();
void push(char ch);
char pop();
private:
char stackData[SIZE];
int topOfStack;
};
stack::stack()
{
cout << "Constructing a stack\n";
topOfStack = 0;
}
void stack::push(char ch)
{
if(topOfStack==SIZE) {
cout << "Stack is full\n";
return;
}
stackData[topOfStack] = ch;
topOfStack++;
}
char stack::pop()
{
if(topOfStack==0) {
cout << "Stack is empty\n";
return 0; // return null on empty stack
}
topOfStack--;
return stackData[topOfStack];
}
int main()
{
stack stackObject1, stackObject2;
int i;
stackObject1.push("a");
stackObject2.push("x");
stackObject1.push("b");
stackObject2.push("y");
stackObject1.push("c");
stackObject2.push("z");
for(i = 0; i <3; i++)
cout << "Pop stackObject1: " << stackObject1.pop() << endl;
for(i = 0; i <3; i++)
cout << "Pop stackObject2: " << stackObject2.pop() << endl;
return 0;
}
use struct to initialize a class
#include <iostream>
#include <string.h>
using namespace std;
struct BookInfo {
char title[64];
char publisher[64];
char author[64];
float price;
int pages;
};
class BookStuff {
public:
BookStuff(char *title, char *publisher, char *author);
BookStuff(struct BookInfo);
void show_book(void)
{ cout << "Book: " << title << " by " <<
author << endl << "Publisher: " << publisher << endl; };
private:
char title[64];
char author[64];
char publisher[64];
};
BookStuff::BookStuff(char *title, char *publisher, char *author)
{
strcpy(BookStuff::title, title);
strcpy(BookStuff::publisher, publisher);
strcpy(BookStuff::author, author);
}
BookStuff::BookStuff(BookInfo book)
{
strcpy(BookStuff::title, book.title);
strcpy(BookStuff::publisher, book.publisher);
strcpy(BookStuff::author, book.author);
}
int main(void)
{
BookInfo book = {"T", "J", "a", 29.95, 256 };
BookStuff big_book("C", "P","K");
BookStuff little_book(book);
big_book.show_book();
little_book.show_book();
}
Using a class instead of struct.
#include <iostream>
using namespace std;
class MyClass {
int i;// private by default
public:
int get_i(void);
void put_i(int j);
} ;
int MyClass::get_i(void)
{
return i;
}
void MyClass::put_i(int j)
{
i = j;
}
main(void)
{
MyClass s;
s.put_i(10);
cout << s.get_i();
return 0;
}
Using a structure to define a class.
#include <iostream>
#include <cstring>
using namespace std;
struct StringStructClass {
void initString(char *s); // public
void displayString();
private: // private
char str[255];
} ;
void StringStructClass::initString(char *s)
{
if(!*s)
*str = "\0"; // initialize string
else
strcat(str, s);
}
void StringStructClass::displayString()
{
cout << str << endl;
}
int main()
{
StringStructClass stringObject;
stringObject.initString("");
stringObject.initString("Hello ");
stringObject.initString("there!");
stringObject.displayString();
return 0;
}