c++ cdi+示例_C ++中带有示例的本地类

c++ cdi+示例

C ++中的本地类 (Local Class in C++)

In C++, generally a class is declared outside of the main() function, which is global for the program and all functions can access that class i.e. the scope of such class is global.

在C ++中,通常在main()函数外部声明一个类,该类对于程序是全局的,并且所有函数都可以访问该类,即此类的范围是全局的。

A local class is declared inside any function (including main() i.e. we can also declare a class within the main() function) and the scope of local class is local to that function only i.e. a local class is accessible within the same function only in which class is declared.

本地类在任何函数内声明(包括main(),即我们也可以在main()函数内声明一个类),并且本地类的范围仅对该函数本地,即本地类只能在同一函数内访问在哪个类中声明。

Example:

例:

Here, we are declaring and defining two classes "Test1" and "Test2", "Test1" is declared inside a user-defined function named testFunction() and "Test2" is declares inside the main() function.

在这里,我们声明并定义了两个类“ Test1”“ Test2” ,在一个名为testFunction()的用户定义函数中声明了“ Test1”,在main()函数中声明了“ Test2”

Since classes "Test1" and "Test2" are declared within the functions, thus, their scope will be local to those functions. Hence, "Test1" and "Test2" are local classes in C++.

由于在函数中声明了“ Test1”“ Test2”类,因此它们的作用域对于这些函数而言是局部的。 因此, “ Test1”“ Test2”C ++中的本地类

Program:

程序:

#include <iostream>
using namespace std;

//A user defined function
void testFunction(void)
{
    //declaring a local class
    //which is accessible within this function only
    class Test1
    {
        private:
            int num;
        public:
            void setValue(int n)
            {
                num = n;
            }
            int getValue(void)
            {
                return num;
            }
    };
    
    //any message of the function
    cout<<"Inside testFunction..."<<endl;
    
    //creating class's object
    Test1 T1;
    T1.setValue(100);
    cout<<"Value of Test1's num: "<<T1.getValue()<<endl;
}

//Main function
int main()
{
    //declaring a local class
    //which is accessible within this function only
    class Test2
    {
        private:
            int num;
        public:
            void setValue(int n)
            {
                num = n;
            }
            int getValue(void)
            {
                return num;
            }
    };
    
    
    //calling testFunction
    cout<<"Calling testFunction..."<<endl;
    testFunction();
    
    //any message of the function
    cout<<"Inside main()..."<<endl;
    
    //creating class's object
    Test2 T2;
    T2.setValue(200);
    cout<<"Value of Test2's num: "<<T2.getValue()<<endl;
    
    return 0;
}

Output

输出量

Calling testFunction...
Inside testFunction...
Value of Test1's num: 100
Inside main()...
Value of Test2's num: 200


翻译自: https://www.includehelp.com/cpp-programs/local-class-with-example.aspx

c++ cdi+示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
void CDIPDlg::OnOpen() { // TODO: Add your command handler code here CString fileName; CFileDialog OpenDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_EXPLORER,"图像文件格式(*.bmp)|*.bmp|JPG file format (*.jpg)|*.jpg|",NULL); if(OpenDlg.DoModal()!=IDOK) return; fileName=OpenDlg.GetPathName(); std::string tempName=(LPCSTR)CString(fileName); const char* tmp=tempName.c_str(); if((pImg=cvLoadImage(tmp,1))==0) return; wImg=cvCreateImage(cvGetSize(pImg),pImg->depth,pImg->nChannels); cvCopy(pImg,wImg,NULL); ShowImage( pImg,IDC_STATIC_P ); ShowOImage( pImg, IDC_STATIC_O ); } void CDIPDlg::OnSave() { // TODO: Add your command handler code here CFileDialog SaveDlg(FALSE,"bmp","未命名",OFN_HIDEREADONLY|OFN_EXPLORER,"图像文件格式(*bmp)|*.bmp|JPG file format (*jpg)|*.jpg|",NULL); if(SaveDlg.DoModal()==IDOK){ CString savefileName=SaveDlg.GetPathName(); std::string tempSaveName=(LPCSTR)CString(savefileName); const char* nameOfFile=tempSaveName.c_str(); if(wImg) cvSaveImage(nameOfFile,wImg); else cvSaveImage(nameOfFile,pImg); MessageBox("ok!"); } else MessageBox("no!"); } void CDIPDlg::OnEsc() { // TODO: Add your command handler code here CDialog::OnDestroy(); cvReleaseImage(&pImg;); cvReleaseImage(&wImg;); cvReleaseImage(&gImg;); cvDestroyAllWindows(); AfxGetMainWnd()->SendMessage(WM_CLOSE); } void CDIPDlg::OnGaos() { // TODO: Add your command handler code here wImg=cvCreateImage(cvGetSize(pImg),pImg->depth,pImg->nChannels); cvCopy(pImg,wImg,NULL); IplImage* in; in = wImg; IplImage* out = cvCreateImage(cvGetSize(in),IPL_DEPTH_8U,wImg->nChannels); // 建立辅助位图 cvSmooth(in,out,CV_GAUSSIAN,3,wImg->nChannels); // Gauss 平滑 // cvNamedWindow("高斯滤波",1); // cvShowImage("高斯滤波",out); ShowImage( out,IDC_STATIC_P ); wImg=cvCreateImage(cvGetSize(out),out->depth,out->nChannels); cvCopy(out,wImg,NULL); cvWaitKey(0); // cvDestroyWindow("高斯滤波"); cvReleaseImage(&in;); cvReleaseImage(&out;); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值