![]() |
|
|
#1 |
|
Rìu Sắt Đôi
![]() Tham gia: Dec 2007
Bài: 226
VZD: 5.167
Điểm: 74/44 bài viết
|
Ai biết tạo file .h trong C ko?? chỉ mình với
Mình xem tài liệu nhưng ko hiểu. Nếu có ví dụ càng tốt. VÍ dụ cả trong .h và .cpp nhé, để mình xem thử làm sao gọi
__________________
Web site lập trình C/C++ http://it9x.co.cc Kiếm tiền bằng sms http://ps.vietguys.biz/network/0989211841 |
|
|
|
|
|
#2 |
|
Rìu Sắt Đôi
![]() |
Tạo thì cứ tạo thôi.
![]() Đọc sách không hiểu thì bó tay rồi. ![]() Muốn dùng nó chỉ cần: PHP Code:
.cpp là file để code rồi. |
|
|
|
|
|
#3 |
|
Rìu Sắt Đôi
![]() Tham gia: Dec 2007
Bài: 226
VZD: 5.167
Điểm: 74/44 bài viết
|
mình biết là thế nhưng trong .h thì đánh code thể nào để xài
__________________
Web site lập trình C/C++ http://it9x.co.cc Kiếm tiền bằng sms http://ps.vietguys.biz/network/0989211841 |
|
|
|
|
|
#4 |
|
Rìu Sắt Đôi
![]() |
Mình lấy tiện một file đây.
![]() PHP Code:
![]() PHP Code:
|
|
|
|
| Thành viên đã cám ơn bài viết này của thienthan: | darling (22-05-2008) |
|
|
#5 |
|
Rìu Sắt Đôi
![]() Tham gia: Mar 2008
Đến từ: Hà Nội
Bài: 272
VZD: 5.687
Điểm: 255/76 bài viết
|
Bạn darling cảm ơn nhầm người rùi! Code của bạn thienthan là code của C++.Net cho Windows Form chứ đâu phải của C! Mình biết khá nhiều ngôn ngữ nên nhận ra ngay.
Sau đây là code cho một file .h của C: Code:
#ifndef MYLIB_H #define MYLIB_H typedef unsigned char BYTE;//Đây là cú pháp yêu cầu tạo ra kiểu mới là BYTE dựa trên kiểu unsigned char extern char stHexa[3]; extern char * ByteToHexa(const BYTE&n); #endif Code:
#ifndef MYSTRING_H
#define MYSTRING_H
class MyString
{
private:
char * buff;
};
#endif
|
|
|
|
|
|
#6 |
|
Rìu Sắt Đôi
![]() |
Mà dst post một bài thôi nếu sai hoặc bổ xung thì edit là được rồi.
Đúng là code đó là của winform nhưng đâu cũng vậy bạn phải nói rõ. Đấy trên VC++ thì nó hơi quái một tí. Bạn ấy hỏi C nhưng nói .cpp thì cũng đủ hiểu là C++ rồi. Code:
// This is the main project file for VC++ application project
// generated using an Application Wizard.
#include "stdafx.h"
#include "Bank.h"
#include "Account.h"
#using <mscorlib.dll>
using namespace System;
// This is the entry point for this application
#ifdef _UNICODE
int wmain(void)
#else
int main(void)
#endif
{
Console::WriteLine(S"Bank example");
// Create a bank
Bank* theBank = new Bank();
// Create some accounts
Account* accountOne = new Account(123456, 100.0, 0.0);
Account* accountTwo = new Account(234567, 1000.0, 100.0);
Account* accountThree = new Account(345678, 10000.0, 1000.0);
// Add them to the Bank
theBank->Add(accountOne);
theBank->Add(accountTwo);
theBank->Add(accountThree);
// Use the indexed property to access an account
Account* pa = theBank->Acct[234567];
Console::WriteLine("Balance is {0}", __box(pa->get_Balance()));
return 0;
}
Code:
#pragma once
#include "Account.h"
#using <mscorlib.dll>
using namespace System::Collections;
__gc class Bank
{
public:
Bank(void);
private:
ArrayList* accts;
public:
bool Add(Account* pAcc)
{
// check if account is in list
if (accts->Contains(pAcc))
return false;
else
accts->Add(pAcc);
return true;
}
bool Remove(Account* pAcc)
{
// check if account is in list
if (accts->Contains(pAcc))
{
accts->Remove(pAcc);
return true;
}
else
return false;
}
__property Account* get_Acct(long number)
{
IEnumerator* ie = accts->GetEnumerator();
while (ie->MoveNext())
{
Account* pa = dynamic_cast<Account*>(ie->get_Current());
if (pa->AccountNumber == number)
return pa;
}
throw new ArgumentOutOfRangeException("Bad account number");
}
};
Hoặc là một vài ví dụ VC++ dùng thuần API đây: Code:
//Tên file là borg2.h #ifndef borg2_h #define borg2_h #include "common.h" #endif Code:
// database.h #ifndef database_h #define database_h void savedb(void); void loaddb(void); #endif Code:
// DASM.H
#ifndef dasmh
#define dasmh
#include "common.h"
#define WM_MAXITOUT (WM_USER+100)
#define WM_REPEATNAMEVIEW (WM_USER+101)
#define WM_REPEATXREFVIEW (WM_USER+102)
#define BORG_VER 228
#define CD_PUSHBP 1
#define CD_ENTER 2
#define CD_MOVBX 4
#define CD_AGGRESSIVE 8
#define CD_EAXFROMESP 16
#define CD_MOVEAX 32
#define VERTSCROLLRANGE 16000
enum fontselection {ansifont=1,systemfont,courierfont,courierfont10,courierfont12};
// nb changing this options structure will change the file database structure
struct globaloptions
{ bool loaddebug;
bool mode16,mode32;
bool loaddata,loadresources;
bool demangle;
bool cfa;
dword processor;
lptr loadaddr,oep;
word dseg;
word codedetect;
COLORREF bgcolor,textcolor,highcolor;
fontselection font;
bool readonly; // file readonly
bool winmax; // Borg window is maximised
};
void StatusMessageNItems(dword nolistitems);
void changemenus(void);
void StatusMessage(char *msg);
void Thread(PVOID pvoid);
extern CRITICAL_SECTION cs;
extern HINSTANCE hInst;
extern HWND mainwindow;
extern globaloptions options;
extern RECT mainwnd;
extern volatile bool KillThread;
extern char winname[];
extern HFONT cf;
extern HANDLE ThreadHandle;
extern DWORD ThreadId;
extern volatile bool InThread;
extern char program_name[];
extern char current_exe_name[];
#define Status_SetText(hwnd, iPart, uType, szText) \
PostMessage((hwnd), SB_SETTEXT, (WPARAM) (iPart | uType), (LPARAM) (LPSTR) szText)
#endif
Mẫu trên bạn cụ thể ra là: Code:
#ifndef brog2_h #define brog2_h //Bạn đưa phần nội dung vào đây. //Kết thúc. #endif thay đổi nội dung bởi: thienthan, 23-05-2008 lúc 08:53. Lý do: Thêm nội dung |
|
|
|
|
|
#7 |
|
Rìu Sắt Đôi
![]() Tham gia: Mar 2008
Đến từ: Hà Nội
Bài: 272
VZD: 5.687
Điểm: 255/76 bài viết
|
Xin lỗi thienthan, nhưng rõ ràng là code 1, 2 của bạn là c++.Net nên mới có namespace System, System::IO,... và câu lệnh Console::ReadLine,... Tôi nói vậy vì trong C++.Net thì kể cả file .h cũng có thể khai báo phần thực thi chương trình, còn trong C++ thì khai báo ở .h, phần thực thi chỉ có thể đặt ở file .cpp, không thể lẫn lộn được. VD: nếu bạn dùng VC++ và chọn kiểu dự án là MFC thì khi bạn đặt mã thực thi cho một nút chẳng hạn, bạn sẽ được đặt vào file .cpp, nếu đặt mã thực thi trong file.h thì báo lỗi, còn trong winform thì đặt ở đâu cũng được. Bạn hiểu chưa?
|
|
|
|