一段有趣的C++代码 -C++和JAVA其实是一样的

第一次写博客 ,感觉有点不好意思 。首先问题一个问题:我原先不知道做什么,所以在大学里什么都学,基础知识也不错,从嵌入式学到应用软件,所以找工作了的时候不知道该找什么样的工作。现在知道了,自己的方向是Linux嵌入式驱动开发。但现在工作已经确定了,是C++客户端开发(和硬件还有一定的联系)。所以只能慢慢自学了。二三年后,自己还可以去应聘Linux驱动开发的职位吗?我是计算机科学与技术专业。对平时电脑都用fedora。所以现在做MFC比较郁闷(应聘的时候以为是做Linux C++的开发)。好了,还是说代码吧!我想大学里面大多数同学都学过JAVA,而且有些学的还不错。为JAVA简单的语法和美妙的特性所折服。而C++总是花费我们太多的时间 。而且一次也没有停止学习。有时候,感觉JAVA和C++在语法上真是太像了。很多都可以作代替性的解释。import 和include就不说了,但是JAVA在类里面可以写主函数这一点怕是让C++很难模仿。工作是突然想到一种在类里面写main函数的方法。试了一下,还真行,现在分享给大家:

#include "stdafx.h"

#include<iostream>

 class A

 {

    friend int main(int argc,char **argv)

      {

              A a;a.Out(); 

             return 0; 

     }

  public : void Out()

 {

    std::cout<<"Hello ,boy "<<std::endl;std::cout<<"现在你相信在类里面也可以执行main函数了吗?呵呵!"<<std::endl;

  }

};

运行结果: Hello,body 现在你相信在类里面也可以执行main函数了吗?呵呵!

而且将main的声明用friend static main()也可以运行,感觉相当好。当然了,明眼的人一看就明白是friend这个关键字在起作用。相信大家都明白。我就不解释了。有时候 ,我相信如果有一个好的编译器。可能所有语言都是一样的。到那个时候,根本就不用再分语言了。要编译成什么要的目标文件,只要用不同的编译选项就可以了。就实际而言。我觉得C和JAVA的语法是面向过程和面向对象的经典。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Kotlin代码: ```kotlin import java.security.KeyPairGenerator import java.security.interfaces.RSAPrivateKey import java.security.interfaces.RSAPublicKey import java.util.Base64 import javax.crypto.Cipher fun main() { val message = "Hello, World!" // Generate RSA key pair val keyPairGenerator = KeyPairGenerator.getInstance("RSA") keyPairGenerator.initialize(2048) val keyPair = keyPairGenerator.generateKeyPair() val publicKey = keyPair.public as RSAPublicKey val privateKey = keyPair.private as RSAPrivateKey // Encrypt message using public key val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding") cipher.init(Cipher.ENCRYPT_MODE, publicKey) val encryptedMessage = cipher.doFinal(message.toByteArray()) val encryptedMessageBase64 = Base64.getEncoder().encodeToString(encryptedMessage) // Decrypt message using private key cipher.init(Cipher.DECRYPT_MODE, privateKey) val decryptedMessage = cipher.doFinal(Base64.getDecoder().decode(encryptedMessageBase64)) val decryptedMessageString = String(decryptedMessage) println("Original message: $message") println("Encrypted message: $encryptedMessageBase64") println("Decrypted message: $decryptedMessageString") } ``` C代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/rsa.h> #include <openssl/pem.h> int main() { const char* message = "Hello, World!"; const int keySize = 2048; // Generate RSA key pair RSA* rsaKeyPair = RSA_generate_key(keySize, RSA_F4, NULL, NULL); RSA* rsaPublicKey = RSAPublicKey_dup(rsaKeyPair); RSA* rsaPrivateKey = RSAPrivateKey_dup(rsaKeyPair); // Encrypt message using public key int encryptedMessageLength = RSA_size(rsaPublicKey); unsigned char* encryptedMessage = (unsigned char*)malloc(encryptedMessageLength); RSA_public_encrypt(strlen(message), (unsigned char*)message, encryptedMessage, rsaPublicKey, RSA_PKCS1_PADDING); char* encryptedMessageBase64 = (char*)malloc(encryptedMessageLength * 2); int encryptedMessageBase64Length = EVP_EncodeBlock((unsigned char*)encryptedMessageBase64, encryptedMessage, encryptedMessageLength); // Decrypt message using private key int decryptedMessageLength = RSA_size(rsaPrivateKey); unsigned char* decryptedMessage = (unsigned char*)malloc(decryptedMessageLength); RSA_private_decrypt(encryptedMessageLength, encryptedMessage, decryptedMessage, rsaPrivateKey, RSA_PKCS1_PADDING); char* decryptedMessageString = (char*)malloc(decryptedMessageLength + 1); memcpy(decryptedMessageString, decryptedMessage, decryptedMessageLength); decryptedMessageString[decryptedMessageLength] = '\0'; printf("Original message: %s\n", message); printf("Encrypted message: %s\n", encryptedMessageBase64); printf("Decrypted message: %s\n", decryptedMessageString); RSA_free(rsaKeyPair); RSA_free(rsaPublicKey); RSA_free(rsaPrivateKey); free(encryptedMessage); free(encryptedMessageBase64); free(decryptedMessage); free(decryptedMessageString); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值