Go Back   VN-Zoom forum > Chia sẻ kiến thức > Lập trình > Pascal | C/C++

 


Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 20-05-2008, 20:35   #1
Rìu Sắt Đôi
 
darling's Avatar
 
Tham gia: Dec 2007
Bài: 226
VZD: 5.167
Điểm: 74/44 bài viết
Default Ai biết tạo file .h trong C ko?? chỉ mình với

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
darling vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 21-05-2008, 18:49   #2
Rìu Sắt Đôi
 
thienthan's Avatar
 
Tham gia: Nov 2007
Bài: 291
VZD: 4.580
Điểm: 86/69 bài viết
Send a message via Yahoo to thienthan
Default

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:
#include <??>//điền tên file vào đây.
#include "??"//Điền đầy đủ đường dẫn đến file vào đây
.h header file thôi.
.cpp là file để code rồi.
thienthan vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 22-05-2008, 05:16   #3
Rìu Sắt Đôi
 
darling's Avatar
 
Tham gia: Dec 2007
Bài: 226
VZD: 5.167
Điểm: 74/44 bài viết
Default

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
darling vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 22-05-2008, 15:43   #4
Rìu Sắt Đôi
 
thienthan's Avatar
 
Tham gia: Nov 2007
Bài: 291
VZD: 4.580
Điểm: 86/69 bài viết
Send a message via Yahoo to thienthan
Default

Mình lấy tiện một file đây.
PHP Code:
#pragma once


namespace Sudoku {

    
using namespace System;
    
using namespace System::ComponentModel;
    
using namespace System::Collections;
    
using namespace System::Windows::Forms;
    
using namespace System::Data;
    
using namespace System::Drawing;

    
/// <summary>
    /// Summary for Form1
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          'Resource File Name' property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    /// </summary>
    
public ref class Form1 : public System::Windows::Forms::Form
    
{
    public:
        
Form1(void)
        {
            
InitializeComponent();
            
//
            //TODO: Add the constructor code here
            //
        
}

    protected:
        
/// <summary>
        /// Clean up any resources being used.
        /// </summary>
        
~Form1()
        {
            if (
components)
            {
                
delete components;
            }
        }

    private:
        
/// <summary>
        /// Required designer variable.
        /// </summary>
        
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        
void InitializeComponent(void)
        {
            
this->components gcnew System::ComponentModel::Container();
            
this->Size System::Drawing::Size(300,300);
            
this->Text L"Form1";
            
this->Padding System::Windows::Forms::Padding(0);
            
this->AutoScaleMode System::Windows::Forms::AutoScaleMode::Font;
        }
#pragma endregion
    
};

Bạn nhớ từ này ở đầu quan trọng nhất phải có đó!
PHP Code:
#pragma once 
thienthan vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Thành viên đã cám ơn bài viết này của thienthan:
darling (22-05-2008)
Old 22-05-2008, 19:52   #5
dst
Rìu Sắt Đôi
 
dst's Avatar
 
Tham gia: Mar 2008
Đến từ: Hà Nội
Bài: 272
VZD: 5.687
Điểm: 255/76 bài viết
Default

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
Còn đây là code cho một file .h của C++:
Code:
#ifndef MYSTRING_H
#define MYSTRING_H
class MyString
{
private:
char * buff;
};
#endif
dst vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 23-05-2008, 08:42   #6
Rìu Sắt Đôi
 
thienthan's Avatar
 
Tham gia: Nov 2007
Bài: 291
VZD: 4.580
Điểm: 86/69 bài viết
Send a message via Yahoo to thienthan
Default

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;
}
Đâu cứ phải winform mới được dùng #pragma once
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");
    }



};
Ví dụ trên là cmd đó.
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
Lưu ý trong .h không nhất thiết phải có class đâu.

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
thienthan vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 23-05-2008, 10:08   #7
dst
Rìu Sắt Đôi
 
dst's Avatar
 
Tham gia: Mar 2008
Đến từ: Hà Nội
Bài: 272
VZD: 5.687
Điểm: 255/76 bài viết
Default

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?
dst vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời

Ðiều Chỉnh
Xếp Bài

Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt
Trackbacks are Tắt
Pingbacks are Tắt
Refbacks are Tắt
Chuyển đến


Múi giờ GMT +7. Hiện tại là 02:13.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
Từ điển được cung cấp bởi VDict.com - Hosting được tài trợ bởi Rao vặt 123